Ruby on rails 为什么我的嵌套属性表单在rails中不起作用?

Ruby on rails 为什么我的嵌套属性表单在rails中不起作用?,ruby-on-rails,haml,erb,nested-forms,nested-attributes,Ruby On Rails,Haml,Erb,Nested Forms,Nested Attributes,我有一个叫profile的模型,它有很多工作。我使用cocoon gem是为了允许用户创建个人资料,然后在单独的页面上创建他们想要的任意多个作业。配置文件表单工作正常。然而,工作形式似乎并没有真正创造就业机会。由于用户需要先填写配置文件表单,然后才能填写作业表单,因此当用户到达作业表单时,它将自动转到配置文件控制器中的更新操作,而不是创建。我很确定问题出在配置文件控制器上。以下是配置文件控制器: def new if current_user.profile redire

我有一个叫profile的模型,它有很多工作。我使用cocoon gem是为了允许用户创建个人资料,然后在单独的页面上创建他们想要的任意多个作业。配置文件表单工作正常。然而,工作形式似乎并没有真正创造就业机会。由于用户需要先填写配置文件表单,然后才能填写作业表单,因此当用户到达作业表单时,它将自动转到配置文件控制器中的更新操作,而不是创建。我很确定问题出在配置文件控制器上。以下是配置文件控制器:

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 %>

乔布斯
工作%>
以下是作业字段部分:

.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 %>
。嵌套字段





问题还可能是我把汉密尔顿语翻译成了ERB语,我认为我做得不对


此外,所有的配置文件实际上都属于一个用户,但我认为这不会有什么区别。提前感谢您的帮助

如果问题是要为
作业
调用
create
方法,则需要修改表单以明确使用
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 %>
new_jobs_path,:method=>:post do | f |%>
乔布斯
工作%>