weixin_33697898 2016-12-25 14:51 采纳率: 0%
浏览 44

Rails:通过链接更新用户

I am trying to update a devise record via a link in my view.

This is what I've have:

= link_to "Update", user_registration_path(current_user, user: {abc: true}), method: :put, remote: true

I get the following issue:

private method `to_param' called for #

How am I able to update this record remotely?

  • 写回答

1条回答 默认 最新

  • weixin_33720452 2016-12-25 15:19
    关注

    When I try to do this what I do is a create a method that the link resolves to like

    routes

    resources :users do 
      match "update_abc" => "update_abc#users", :as => :update_abc, via: :get
    end
    

    now in your view you can do

    users_update_abc_path(current_user, abc: true, efg: 21)
    

    and in the controller

    def update_abc
      user = user.find(params[:id])
      if params[:abc].present?
        user.abc = params[:abc]
      end
      ....
      user.save
      redirect_to :back
    end
    

    I hope that this helps :)

    评论

报告相同问题?