【问题标题】:PATCH method redirects to destroy action rather than update actionPATCH 方法重定向到销毁动作而不是更新动作
【发布时间】:2018-09-25 13:21:51
【问题描述】:

在我的应用程序中,我试图通过表单更新用户信息。但是,我能够呈现用户的详细信息,同时更新它重定向以销毁操作的事件并显示以下错误。 没有路线匹配 [PATCH] "/dashboards/parameter/destroy"

routes.rb

    resources :users
    resources :suggestions do
    member do
      get "follow", to: "suggestions#follow", constraints: {id: /[^\/]+/ }

    end
  end


  resources :dashboards, constraints: {id: /[^\/]+/ }, only: [:new,:create,:index,:edit,:update]  do
    member do
      get "like", to: "dashboards#like"
      get "logout", to: "dashboards#logout",constraints: {id: /[^\/]+/ }
      delete 'destroy' => 'dashboards#destroy',constraints: {id: /[^\/]+/ }
    end
  end
  match 'showposts' => 'dashboards', :via => [:get], :as => 'showposts'

dashboards_contoller.rb

def edit
    if params[:id] == session[:user_name]
      @user = User.new
      @user = User.find_by(email: session[:user_name])
    else
        redirect_to edit_dashboard_path(session[:user_name]),danger: "Unauthorized Activity ! The Account you are trying to manipulate doesnot belong to you !"
    end
  end




     def update
        @user = User.find_by(email: session[:user_name])
@user.update_attributes(params.require(:user).permit(:first_name,:last_name,:pas 
 sword,:dob,:contactno))

    end

视图/仪表板内的edit.html.erb

<%= form_for @user, url: dashboard_path, method: :patch do |f| %>
      <input type="hidden" name="page" value="register">
        <%= render(:partial => 'users/registration', :locals => {:f => f} ) %>
        <%= f.submit('Update Profile') %>
      <% end %>

仪表板控制器的路由

 like_dashboard GET    /dashboards/:id/like(.:format)    dashboards#like {:id=>/[^\/]+/}
 logout_dashboard GET    /dashboards/:id/logout(.:format)  dashboards#logout {:id=>/[^\/]+/}
        dashboard DELETE /dashboards/:id/destroy(.:format) dashboards#destroy {:id=>/[^\/]+/}
       dashboards GET    /dashboards(.:format)             dashboards#index
                  POST   /dashboards(.:format)             dashboards#create
    new_dashboard GET    /dashboards/new(.:format)         dashboards#new
   edit_dashboard GET    /dashboards/:id/edit(.:format)    dashboards#edit {:id=>/[^\/]+/}
                  PATCH  /dashboards/:id(.:format)         dashboards#update {:id=>/[^\/]+/}
                  PUT    /dashboards/:id(.:format)         dashboards#update {:id=>/[^\/]+/}
        showposts GET    /showposts(.:format)              dashboards#showposts

非常感谢。

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    您需要在表单中添加 ID 作为隐藏字段,如下所示:

        <%= form_for @user, url: dashboard_path, method: :patch do |f| %>
              <%= f.hidden_field :id %>
              <input type="hidden" name="page" value="register">
                <%= render(:partial => 'users/registration', :locals => {:f => f} ) %>
                <%= f.submit('Update Profile') %>
              <% end %>
    

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 2018-09-06
      • 2013-08-23
      • 1970-01-01
      • 2022-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多