2013-11-01 94 views
0

我有一個稱爲has_many作業的模型。我使用繭寶石爲了讓用戶能夠製作個人資料,然後在單獨的頁面上創建儘可能多的工作。個人資料表單工作正常。但是,這份工作表似乎並沒有創造就業機會。由於用戶在填寫工作表格之前需要填寫個人檔案表格,所以在他們到達工作表格之前,他們會自動轉到配置文件控制器中的更新操作,而不是創建。我很確定問題出在配置文件控制器中。這裏是配置文件控制器:爲什麼我的嵌套屬性表單在rails中不起作用?

def new 
    if current_user.profile 
     redirect_to edit_profile_path(current_user.profile_name) 
    else 
     @profile = Profile.new 
    end 
end 


def create 
    @profile = current_user.build_profile(profile_params) 
    @profile.save 
    if current_user.profile.invalid? 
     render :new, :status => :unprocessable_entity 
    else 
     redirect_to profile_path(current_user.profile_name) 
    end 
end 

def edit 
    @profile = current_user.profile 
end 


def update 
    #if current_user.profile.jobs.any? 
     @profile_save = current_user.profile.update_attributes(profile_params) 

     if current_user.profile.invalid? 
      @profile = current_user.profile 
      render :edit, :status => :unprocessable_entity 
     else 
      redirect_to profile_path(current_user.profile_name) 
     end 
end 

private 
def profile_params 
     params.fetch(:profile, {}).permit(:title, 
      :category, :description, :state, :zip_code, :rate, 
      jobs_attributes: [:firm, :position, :category, :description, 
      :begin, :end, :_destroy]) 
end 

我使用fetch而不是require,否則我收到錯誤說沒有找到配置文件。這裏是形式:

<%= simple_form_for @profile do |f| %> 
    <h3> Jobs </h3> 
    <%= f.simple_fields_for :jobs do |job| %> 
    <%= render 'job_fields', :f => job %> 
     <% end %> 
    <%= link_to_add_association 'add job', f, :jobs %> 
    <%= f.submit %> 
<% end %> 

這裏是job_fields部分:

.nested-fields 
<%= f.input :firm, label: "Firm" %> <br> 
    <%= f.input :position, label: "Position" %> <br> 
    <%= f.input :category, label: "Category"%><br> 
    <%= f.input :begin, label: "Beginning", collection: 1960..2013 %><br> 
    <%= f.input :end, label: "End", collection: 1960..2013 %> 
    <%= f.input :description, label: "Description"%><br> 

    <%= link_to_remove_association "remove task", f %> 

的問題也可能是我從HAML翻譯成ERB,我想我做到了不正確。

此外,所有配置文件實際上屬於一個用戶,但我不認爲這應該有所作爲。先謝謝您的幫助!

回答

0

如果問題出在您想要撥打Jobscreate方法,則需要修改表單以明確使用post方法。例如:

<%= simple_form_for @profile, :url => new_jobs_path, :method => :post do |f| %> 
    <h3> Jobs </h3> 
    <%= f.simple_fields_for :jobs do |job| %> 
    <%= render 'job_fields', :f => job %> 
     <% end %> 
    <%= link_to_add_association 'add job', f, :jobs %> 
    <%= f.submit %> 
<% end %> 
相關問題