PATCH方法重定向以销毁操作而不是更新操作

时间:2018-04-15 20:35:55

标签: ruby-on-rails routes

在我的应用程序中,我试图通过表单更新用户信息。我能够呈现用户的详细信息,但在更新事件时,它会重定向到销毁操作并显示以下错误。 没有路由匹配[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

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

您需要在表单中添加 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 %>