2011-05-12 58 views
3

我有一个表单,当我在编辑操作期间提交时,会将.id附加到后操作,当它不应该时。表单在创建过程中正常工作,但不能更新。form_for操作在编辑操作期间在URL中附加了.id - Rails 3

以下是编辑期间的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 %> 

这是我对这个协会的路线。

resources :members do 
    resource :profile 
    resources :orders 
end 

这里是我的创建,编辑和更新我的配置控制器

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 

什么添加profile.id到后行动的行动?

回答

1

我相信这有时会发生在嵌套奇异资源中。尝试使用不同的form_for格式:

form_for @profile, :url => member_profile_path(@member) do |f| 
+0

对。它将'@ profile'视为url的':format'选项。 – 2011-05-12 00:43:49

+0

你能否更清楚地澄清这个问题 - 比如说,为什么会发生这种情况?我遇到了同样的问题,现在很难尝试应用您的解决方案。 – artshpakov 2012-07-24 14:48:57

+1

当你有嵌套奇异资源时,url助手不需要传递单数资源对象_因为它知道只有一个对象可以选择from_。所以例如'member_profile_path(@member,@profile)'应该成为'member_profile_path(@member)',因为你所谈论的配置文件(每个成员只有一个)没有歧义。与'form_for'类似。 – 2013-06-26 23:42:29