我有一个名为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 %>
这是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 :(得分:0)
如果问题是您要为create
调用Jobs
方法,则需要修改表单以明确使用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 %>