Unable to connect Devise User to ProfilesController

38 views
Skip to first unread message

David Williams

unread,
Dec 18, 2014, 5:26:35 PM12/18/14
to rubyonra...@googlegroups.com
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
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.

--
Posted via http://www.ruby-forum.com/.

James Davis, PhD

unread,
Dec 19, 2014, 8:18:53 AM12/19/14
to rubyonra...@googlegroups.com
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.

David Williams

unread,
Dec 19, 2014, 11:14:51 AM12/19/14
to rubyonra...@googlegroups.com
James Davis, PhD wrote in post #1164975:
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

<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: "delete"
%></li>
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.

David Williams

unread,
Dec 19, 2014, 11:29:39 AM12/19/14
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Dec 19, 2014, 11:50:28 AM12/19/14
to rubyonra...@googlegroups.com
On 19 December 2014 at 16:29, David Williams <li...@ruby-forum.com> 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

David Williams

unread,
Dec 19, 2014, 12:17:53 PM12/19/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1164987:
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 Law

unread,
Dec 19, 2014, 12:24:58 PM12/19/14
to rubyonra...@googlegroups.com
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

David Williams

unread,
Dec 19, 2014, 1:04:35 PM12/19/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1164989:
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.

Colin Law

unread,
Dec 19, 2014, 1:20:03 PM12/19/14
to rubyonra...@googlegroups.com
Have a look at the rails guide on routing, you can use match to make
up any route you like.

Colin

Vivek Sampara

unread,
Dec 20, 2014, 2:02:00 AM12/20/14
to rubyonra...@googlegroups.com
David for the route to show up like twitter, you can use friendly_id gem. or write your own route like this

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




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsK8rWQStm910uSpMFcGkX2h8-pHgy7hiA1u1ZC-k1JcA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Dec 20, 2014, 2:32:43 AM12/20/14
to rubyonra...@googlegroups.com
Vivek Sampara 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.
Reply all
Reply to author
Forward
0 new messages