form_for 操作在编辑操作期间将 .id 附加到 URL - Rails 3

Rob*_*t B 5 ruby-on-rails

我有一个表单,当我在编辑操作期间提交时,将 .id 附加到不应该的 post 操作。表单在创建但不更新期间正常工作。

这是编辑期间的 URL 发布操作。

http://localhost:3000/members/1/profile.1

这是我的表格

<%= form_for([@member, @profile]) do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, {:class => "txt-field-short"} %><br /><br />
<%= f.label :last_name %><br />
<%= f.text_field :last_name, {:class => "txt-field-short"} %><br /><br />
<p><%= submit_tag "Create Profile" %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这是我参加这个协会的路线。

resources :members do
  resource :profile
  resources :orders
end
Run Code Online (Sandbox Code Playgroud)

这是我在配置文件控制器中的创建、编辑和更新操作

def create
 @member = current_member
 @profile = @member.build_profile(params[:profile])

 respond_to do |format|
  if @profile.save
    format.html { redirect_to(member_profile_path, :notice => 'Profile was successfully     created.') }
  else
    format.html { render :action => "new" }
  end
end
end

def edit
 @member = current_member
 @profile = @member.profile
end

def update
 @member = current_member
 @profile = @member.profile
 respond_to do |format|
   if @profile.update_attributes(params[:profile])
     format.html { redirect_to(member_profile_path(@profile), :notice => 'Your profile was successfully updated.') }
  else
    format.html { render :action => "edit" }
   end
 end
end
Run Code Online (Sandbox Code Playgroud)

什么将 profile.id 添加到 post 操作?

Dyl*_*kow 2

我相信嵌套单一资源有时会发生这种情况。尝试使用不同的form_for格式:

form_for @profile, :url => member_profile_path(@member) do |f|
Run Code Online (Sandbox Code Playgroud)

  • 当您有嵌套单一资源时,url 帮助器不需要传递单一资源对象_因为它知道只有一个对象可供选择_。因此,例如 `member_profile_path(@member, @profile)` 应该变成 `member_profile_path(@member)` 因为您正在谈论的配置文件没有歧义(每个成员只有一个)。与“form_for”类似。 (2认同)