Ray
Ray

Reputation: 9674

Link to user's profile view

How can I link to a user's profile view without getting a no implicit conversion of Fixnum into String error?

What I currently have:

- @questions.each do |question|
  = link_to question.user.username,  "/users/"+question.user.id

Basically what I am trying to achieve is:

When the user clicks on a link, he will get redirected to the original poster's profile page. ex: localhost:3000/users/1

Home controller:

  def profile
    if !user_signed_in?
        redirect_to new_user_session_path
    end
    if User.find_by_id(params[:id])
      @user = User.find(params[:id])
    else
      redirect_to root_path
    end
  end

Routes:

Rails.application.routes.draw do
  root 'home#home'
  devise_for :users
  get "/users/:id" => "home#profile"
  get "home" => "home#feed"
  get "questions" => "home#questions"
  resources :questions
end

Upvotes: 1

Views: 300

Answers (2)

7urkm3n
7urkm3n

Reputation: 6311

Also this one should work.

= link_to question.user.username,  "/users/#{question.user.id}"

Upvotes: 1

Gerald
Gerald

Reputation: 814

You need to cast the id manually to a string:

  = link_to question.user.username,  "/users/"+question.user.id.to_s

I hope that helps.

Upvotes: 2

Related Questions