0

I'm using the acts_as_follower gem. I'm also using Devise for my user model and instead of a format like users/:id, I've allowed users to have username URLs. I'm working on allowing users to follow each other, but after a user is followed, Rails fails to redirect to the user's profile. Here's my code.

User Controller

    def show
       @user = User.find_by_username(params[:id].downcase) 
    end

    def follow
       @user = User.find(params[:id])
       if current_user
         current_user.follow(@user)
         redirect_to @user.username
       else 
          flash[:error] = "You Must be logged in"
       end
    end

Here's the error that I'm getting in my server.

  ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000alaxics (or bad hostname?)
Alex Smith
  • 191
  • 2
  • 9

2 Answers2

0

You probably want something more like

redirect_to user_path(@user.username)

Or if you're intent on writing the path yourself, just redirect_to "/users/#{@user.username}"

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
0

Remove the .username, just redirect_to @user should work as expected.

Whatever you pass to redirect_to has to be a valid path, and any string without at least a / in front won't be valid when appended to your root_url. This is exactly the error you're getting.

If you just redirect_to @model, that's a rails shortcut for redirect_to model_path(@model), which is what you need.

Andrew
  • 42,517
  • 51
  • 181
  • 281