【发布时间】:2016-06-22 05:00:04
【问题描述】:
我的 rails 应用程序中有一个带有自定义 URL 路由的用户对象。当我提交更新表单时,由于某种原因,用户 ID 被附加到 URL,并且出现路由错误。
路线:
get 'myaccount' => 'users#show', as: 'user'
get 'myaccount/edit' => 'users#edit'
patch 'myaccount/edit' => 'users#update'
put 'myaccount/edit' => 'users#update'
查看:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', :form_object => @user %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
控制器动作:
def update
@user = User.find_by(id: current_user.id)
if @user.update_attributes(user_params)
# successful update
else
# unsuccessful update
end
end
错误: 提交后,网址为“myaccount.7”,我收到错误消息: 路由错误 没有路由匹配 [PATCH] "/myaccount.7"
【问题讨论】:
标签: ruby-on-rails routing custom-url