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到後行動的行動?
對。它將'@ profile'視爲url的':format'選項。 – 2011-05-12 00:43:49
你能否更清楚地澄清這個問題 - 比如說,爲什麼會發生這種情況?我遇到了同樣的問題,現在很難嘗試應用您的解決方案。 – artshpakov 2012-07-24 14:48:57
當你有嵌套奇異資源時,url助手不需要傳遞單數資源對象_因爲它知道只有一個對象可以選擇from_。所以例如'member_profile_path(@member,@profile)'應該成爲'member_profile_path(@member)',因爲你所談論的配置文件(每個成員只有一個)沒有歧義。與'form_for'類似。 – 2013-06-26 23:42:29