Unable to connect Devise User to ProfilesController

The normal devise routes work users_sign_out_path,
edit_user_registration_path, etc
I can’t seem to route to the controller for profiles. I’d like to use
the index/show actions to display the user profile publicly.

Here is my controller setup.

class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]

GET /profiles

GET /profiles.json

def index
@profiles = Profile.all
end

GET /profiles/1

GET /profiles/1.json

def show
@user = User.find(params[:id])

respond_to do |format|
  format.html
  format.json {render :json => @user }
end

end

GET /profiles/1/edit

def edit
end

PATCH/PUT /profiles/1

PATCH/PUT /profiles/1.json

def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: ‘Profile was
successfully updated.’ }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status:
:unprocessable_entity }
end
end
end

DELETE /profiles/1

DELETE /profiles/1.json

def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: ‘Profile was
successfully destroyed.’ }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between
actions.
def set_profile
@profile = Profile.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the

white list through.
def profile_params
params[:profile]
end
end

routes.rb
resources :profiles
profiles GET /profiles(.:format)
profiles#index
POST /profiles(.:format)
profiles#create
new_profile GET /profiles/new(.:format)
profiles#new
edit_profile GET /profiles/:id/edit(.:format)
profiles#edit
profile GET /profiles/:id(.:format)
profiles#show
PATCH /profiles/:id(.:format)
profiles#update
PUT /profiles/:id(.:format)
profiles#update
DELETE /profiles/:id(.:format)
profiles#destroy

It gives me a method undefined error when I use the links for the
controller. Help me figure this out please. I’m pretty sure someone has
come across this problem.

Can you show us the actual error from the console or logs? Also show us
the code where you reference profiles_path and profile_path.

Without seeing this code for context, try moving ‘resources :profiles’
to the top of the routes file as Rais uses the first matching route and
you may have a conflict.

James Davis, PhD wrote in post #1164975:

Can you show us the actual error from the console or logs? Also show us
the code where you reference profiles_path and profile_path.

Without seeing this code for context, try moving ‘resources :profiles’
to the top of the routes file as Rais uses the first matching route and
you may have a conflict.

I destroyed the ProfilesController, so that I can try a new approach. I
found this solution below. Create a new UsersController with show
action. resources :users

class UsersController < ApplicationController
#before_filter :authenticate_user!

def show
@user = User.find(params[:id])

respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @user }
end

end
end

  • <%= link_to "Profile", current_user %>
  • <%= link_to "Settings", edit_user_registration_path %>
  • <%= link_to "Logout", destroy_user_session_path, method: "delete" %>
  • If you have a better way of solving this problem. Please, let me know.
    Devise comes with just about everything you might need, but adding user
    profiles for public and private viewing is a different story.

    On 19 December 2014 at 16:29, David W. [email protected]
    wrote:

    The solution I just posted worked. As in it shows the user profile of
    the person signed in. I may just have to create a ProfilesController to
    show other users that are signed in. The poster says.

    If you want to show which users are currently logged in then why not
    put that in the users controller? That sounds like the most logical
    place to put it.

    Colin

    The solution I just posted worked. As in it shows the user profile of
    the person signed in. I may just have to create a ProfilesController to
    show other users that are signed in. The poster says.

    On 19 December 2014 at 17:17, David W. [email protected]
    wrote:

    during signup for normal users. They have to put both their :username
    and :email in. How could I show just /:username when a person routes to
    their own show page?

    I think I must be misunderstanding the question, you can show as much
    or as little as you like, just put what you want in the view, and
    leave out what you do not want.

    Colin

    Colin L. wrote in post #1164987:

    On 19 December 2014 at 16:29, David W. [email protected]
    wrote:

    The solution I just posted worked. As in it shows the user profile of
    the person signed in. I may just have to create a ProfilesController to
    show other users that are signed in. The poster says.

    If you want to show which users are currently logged in then why not
    put that in the users controller? That sounds like the most logical
    place to put it.

    Colin

    True. I will most likely do so. The last issue that I’m having is with
    the route name. It’s presenting the users/:id instead of profile.

    resources :users do
      get 'users/:id' => 'users#show', :as => 'profile'
    end
    

    In all truth, I’d really like to present the :username which I included
    during signup for normal users. They have to put both their :username
    and :email in. How could I show just /:username when a person routes to
    their own show page?

    Colin L. wrote in post #1164989:

    On 19 December 2014 at 17:17, David W. [email protected]
    wrote:

    during signup for normal users. They have to put both their :username
    and :email in. How could I show just /:username when a person routes to
    their own show page?

    I think I must be misunderstanding the question, you can show as much
    or as little as you like, just put what you want in the view, and
    leave out what you do not want.

    Colin

    No, I’m beyond the view issue. I was just trying to fix the route as it
    displays the users [:id] instead of the [:username]. For instance
    twitter.com/username on show page for user. Thanks for your help so far.

    David for the route to show up like twitter, you can use friendly_id
    https://github.com/norman/friendly_id gem. or write your own route
    like
    this

    get “/:profile_id” => “profiles#show”, :as => “user_profile”,
    :constraints
    => { :profile_id => /[^/]+/ }

    Vivek S. wrote in post #1165021:

    David for the route to show up like twitter, you can use friendly_id
    https://github.com/norman/friendly_id gem. or write your own route
    like
    this

    get “/:profile_id” => “profiles#show”, :as => “user_profile”,
    :constraints
    => { :profile_id => /[^/]+/ }

    I started using this gem earlier today. It works great. All I had to do
    was add the :slug to the :users table and then I added the initializing
    statement to the user.model. I’m currently looking into social
    networking gems, I’m trying to pick the best one before I dive in with
    the project.

    On 19 December 2014 at 18:03, David W. [email protected]
    wrote:

    leave out what you do not want.

    Colin

    No, I’m beyond the view issue. I was just trying to fix the route as it
    displays the users [:id] instead of the [:username]. For instance
    twitter.com/username on show page for user. Thanks for your help so far.

    Have a look at the rails guide on routing, you can use match to make
    up any route you like.

    Colin