user1009762
user1009762

Reputation: 199

Rails 3: when no current_user(no user logged in) friendly_id sends username as :id but controller method does not work

I'm using friendly_id gem and it's working properly when I go to a user's profile page as a logged in user:

localhost/users/facebookname

When I log out and go to the profile page it displays the url same way, no problem. However, when I attempt to display the user's 'liked' posts I run into a problem. Nothing happens.

Started GET "/users/facebookname/likes" for 127.0.0.1 at 2015-01-08 14:47:23 +0700
  Processing by UsersController#likes as JS
  Parameters: {"id"=>"facebookname"}
Completed   in 152ms

UsersController#likes

  def likes
    @title = "Likes"
    @user = User.find(params[:id])

    respond_to do |format|
      format.html { render "posts/posts", :locals => { :posts => @likes } }
      format.js { respond_with @likes}
    end
  end

UsersModel

has_friendly_id :username, :use_slug => true, :approximate_ascii => true

When I am logged in as a user, and try the same thing, the Parameters: {"id"=>"facebookname"} are used the same way, but the controller action uses this with the proper :id and renders @likes. Why is this not happening without a logged in user?

Thank you very much for any insight

Upvotes: 0

Views: 57

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51191

You should find user by username in your controller:

@user = User.find_by_username!(params[:id])

Upvotes: 1

Related Questions