【问题标题】:form_for action has .id appended to URL during edit action - Rails 3form_for 操作在编辑操作期间将 .id 附加到 URL - Rails 3
【发布时间】:2011-05-12 00:25:46
【问题描述】:

我有一个表单,当我在编辑操作期间提交时,会在不应该的情况下将 .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 %>

这是我建立这个协会的途径。

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 添加到 post 操作中?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

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

    【讨论】:

    • 对。它将@profile 视为url 的:format 选项。
    • 您能否进一步澄清这个问题——比如,为什么会发生这种情况?我遇到了同样的问题,现在很难尝试应用您的解决方案。
    • 当你嵌套了单数资源时,url helper 不需要传递单数资源对象因为它知道只有一个对象可供选择。所以例如member_profile_path(@member, @profile) 应该变成 member_profile_path(@member),因为您所谈论的个人资料没有歧义(每个成员只有一个)。与form_for类似。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多