【问题标题】:Updating object parameters appends ID to URL & throws error更新对象参数会将 ID 附加到 URL 并引发错误
【发布时间】: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


    【解决方案1】:

    您有自定义路由,Rails 对将表单发送到哪里以及使用哪种方法感到困惑。您应该明确指定 urlmethod 选项。

     <%= form_for(@user, url: 'myaccount/edit', action: :put)  do |f| %>
    

    Docs.

    【讨论】:

    • 唯一的补充是 url 变量需要是 '/myaccount/edit' 和第一个字符 '/' 否则帖子 URL 被呈现为 '/myaccount/myaccount/edit'
    【解决方案2】:

    试试这个

    resources :myaccount, controller: 'users'
    

    输出:

      myaccount_index GET    /myaccount(.:format)                                                                users#index
                                             POST   /myaccount(.:format)                                                                users#create
                               new_myaccount GET    /myaccount/new(.:format)                                                            users#new
                              edit_myaccount GET    /myaccount/:id/edit(.:format)                                                       users#edit
                                   myaccount GET    /myaccount/:id(.:format)                                                            users#show
                                             PATCH  /myaccount/:id(.:format)                                                            users#update
                                             PUT    /myaccount/:id(.:format)                                                            users#update
                                             DELETE /myaccount/:id(.:format)                                                            users#destroy
                                             GET    /myaccount(.:format)               
    

    【讨论】:

    • 这并没有实现我从 URL 路径中删除 ID 变量的最初目标。我希望 GET myaccount/:id 只是 '/myaccount' 而编辑是 '/myaccount/edit' 而不是 '/myaccount/:id/edit'
    • 没有id怎么识别,是show,editordelete
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2019-07-09
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多