开发者

Editing the current User in Rails 3

开发者 https://www.devze.com 2023-02-21 22:54 出处:网络
Users can be edited from a normal resourceful URI like: /users/1/edit The issue is that in my application, the edit user page is the home page or root route.

Users can be edited from a normal resourceful URI like:

/users/1/edit

The issue is that in my application, the edit user page is the home page or root route.

# routes.rb

root :to => "users#edit"

So, I tried to set @user to the current user in the absence of params[:id].

# app/controllers/users_controller.rb

def edit
  @user = (params[:id]) ? User.find_by_id(params[:id]) : @current_user
end

Unfortunately, I'm having trouble getting the form to point properly.

# app/views/shared/_manage_users.rb

<%= form_tag follow_user_path, :id => 'update-following-form' %>

I'm getting:

No route matches {:action=>"follow", :controller=>"users"}

follow is a member route of the user resource and has a corresponding controller method. If I access the page via the URI at the top of this question, /users/1/edit, everything works fine and no error is thrown.

I'm not sure whether I'm going about this completely the wrong way or if I'm just not using the right form helper or something silly. 开发者_如何学运维How can I fix this issue, or what steps can I follow to debug it?


A member route expects the member to be passed as an argument. You route is expecting a User, like so:

follow_user_path(@user)


in your routes do this

resource :user

instead of

resources :users

now the id param is notin the url. you just need to ensure the user is logged in


I think you need to actually define follow_user in your routes.rb.

Example:

post "user/follow" => "users#follow", :as => :follow_user

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号