Rails routes url take out user_id - ruby-on-rails

I currently have the standard
get 'profile/:id/view', to: 'profile#view', as: 'view'
which produces http://localhost:3000/profile/233/view
the controller is
def view
#user = User.find(params[:id])
end
How can I make a path that the url is http://localhost:3000/profile/view - leaving out the URL?
I was trying to do something like
def view(user_id)
#user = User.find(user_id)
end
But I'm not sure how to write that in the routes, or what the link_to path would look like for that?
I have been looking for a straight forward answer, but have not been able to find one.
Thanks in advance!

You can change the route to
get 'profile/view', to: 'profile#view'
And then in the controller, you can user current_user method which devise provides.
def view
#user = current_user
end

Related

Getting param from friendly url like http://localhost:3000/user/adem-balka with Rails 5

So I want to display posts for an user in his/her profile page as at top user details, below all the posts.
I know I can get param from a url like http://localhost:3000/posts?category=article
with
if params[:category]
#category_id = Category.find_by(title: params[:category]).id
#posts = Post.where(category_id: #category_id)
end
but param doesn't work when I have an url like http://localhost:3000/user/adem-balka
So, how can I get user name to find its id and pull posts with that user id?
Thank you all.
The name of a parameter in a url is set in your routes file.
If you look at your routes in config/routes.rb, you should be able to find the line(s) that corresponds to the user model. It should look something like this:
get '/users/:name', to: 'users#show'
This means that if you go to /users/adem-balka, params[:name] will be set to 'adem-balka'. You can then access the parameter in the corresponding controller function.
What you are looking for is a path parameter, where adem-balka is say params[:username].
Assuming you have no forward slashes or dots in your parameter, this is as simple as adding /:username as part of your route, e.g.
get '/users/:username', to: 'users#show'
# in the controller
#user = User.find_by(username: params[:username])
This is all covered in the Rails Routing from the Outside In guide.
Note that the routes generated resources already contain the :id path parameter for you (for show, edit, etc.). But even if you change the controller, the generated helpers (e.g. users_path(#user)) will use the id.
To make it work with resources using say username instead of id however (e.g. users_path(#user) giving /users/ben instead of /users/5), you need to also override the to_param method, e.g.
class User < ApplicationRecord
def to_param
username #rather than id
end
end
# routes.rb
get '/users/:username' => 'users#posts'
# users_controller.rb
def posts
username = params[:username]
# etc..
end
This is described in the Rails Docs as Routing Parameters.
thank you for the answers. I learned from them 🙏
and this solved my problem
#user_id = User.friendly.find(params[:id])
#posts = Post.where(user_id: #user_id)

Rails 4 - Couldn't find User without an ID

I'm new to rails, so any explanation & advise would much appreciated.
i have a webpage in which i would like any user to view that page not just the current_user, but i am unsure how to correctly define the instance variable #user in my controller
in my static_pages_controller.rb i have the below action recruiterpg
static_pages_controller.rb
def recruiterpg
#user = User.find(params[:user_id])
#adverts = #user.adverts
#applications = #user.forms
end
in my controller, i have defined user as #user = User.find(params[:user_id]) but this breaks my code in the views; views/static_pages/recruiterpg.html.erb
when i define user as #user = current_user my code in the views works perfectly fine
what am trying to do is: for my views, the recruiterpg.html.erb, i would like
any user to be able to view the page not only the current_user to
view the page. Could one kindly advise me and explain to me how to
define #user correctly in my status_pages_controller.rb. i also
tried #user = User.find(params[:id]) but my code still breaks in the
views - i get the error message
Couldn't find User without an ID
You need to make sure you are passing a user_id to the recruiterpg action. For example, try this url in your browser (set user_id to a known id in the users table):
http://localhost:3000/dashboard?user_id=1
A suggested modification to your action:
def recruiterpg
#user = User.find params.require(:user_id)
#adverts = #user.adverts
#applications = #user.forms
end
If params[:user_id] isn't defined, you want to find a way to make visible what is being defined.
If you throw the following statements into your controller...
def recruiterpg
...
puts params
...
end
...you should see something like the following get spit out in your console when you load the page...
{"controller"=>"static_pages", "action"=>"recruiterpg", "id"=>"49"}
Take a look at the Rails guide for parameters. They can get defined in one of three ways.
One: As a query string similar to Sean's answer above.
Two: Routing parameters. See section 4.3 in the Rails guide. In your case, that would mean you should have something like the following in routes.rb:
get '/dashboard/:user_id' => 'staticpages#recruiterpg'
Note that there's nothing magic about :user_id in that string.
Three: From a form which it doesn't seem like applies here, since a user isn't submitting data.
Since you're new, here is some information for you:
User Story
Firstly, the best way to resolve errors is to identify your user story.
A "user story" is a software principle in which you put the "user's perspective" first -- explaining how the software should work in conditions defined from how the user engages with it.
One of the main issues you have with your question is your user story is very weak; it's hard to decifer what you're trying to achieve.
Next time you ask a question, you should try your hardest to describe how the user should see your app, before providing code snippets :)
Controller
Your main issue is an antipattern.
An antipattern is basically a "hack" which will likely break another part of your app in future. Kind of like duct tape for software):
#app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def recruiterpg
#user = User.find(params[:user_id])
#adverts = #user.adverts
#applications = #user.forms
end
end
So you're showing a static page but yet you want to populate it with data?
Hmm...
What you should be doing is something like the following:
#config/routes.rb
resources :users do
resources :recruiters, only: :index #-> url.com/users/:user_id/recruiters
end
#app/controllers/recruiters_controller.rb
class RecruitersController < ApplicationController
def index
#user = User.find params[:user_id]
#adverts = #user.adverts
#applications = #user.forms
end
end
This will allow you to populate the following view:
#app/views/recruiters/index.html.erb
<%= #adverts %>
--
It's important to note the structure of the controller / routes here.
The issue you have is that you're calling a "static page" and expecting to have params available to find a User. This can only happen if you have params available...
Params
Rails is not magic, and as such if you want to look up a user, you have to provide the parameters to do so.
This is why you're able to look up current_user -- the params are already set for this user.
As such, you'll need to use something called nested routes in order to attain a user ID other than that of current_user:
#config/routes.rb
resources :users do
resources :recruiters #-> url.com/users/:user_id/recruiters
end

Masking controllers functions -- Rails3

I would like to hide or mask example.com/users/$ID/edit to be example.com/profile/edit.
config/routes.rb
resources :users
app/controllers/users_controller.rb
def edit
#user = User.find(params[:id])
end
I've tried adding the route
match '/profile/edit' => 'users#edit', :as => :edit_profile
but, when I visit example.com/profile/edit, the edit method complains about not being able to find the user's ID.
Is there a way I can mask the ID from the browser?
If you're trying to edit the current user's profile you could use the following in your edit action:
def edit
#user = User.find_by_id( params[:id] ) || current_user
end
This will first try to look up the user by the id parameter returning nil if it can't find the user and if the return value is nil it will set #user to the return value of the current_user helper method. This assumes you're using something like Devise which provides the current_user method to get the currently logged in user.
One other note. You should change match in your route to get to specify that only get requests are valid for the edit action.
get 'profile/edit' => 'users#edit' , as: edit_profile
If you're trying to prevent users from editing other people's profiles you need something like the following after you load the user:
redirect_to( root_path ) and return unless #user == current_user
This will keep the current user from editing another user's profile.
If you want to allow logged in user to change it's profile, maybe you'll want to store user's id in the session.
So when user logs in you save it's ID into the session:
session[:user_id] = ...
Then your edit method will look like
def edit
#user = User.find(session[:user_id])
end
If that solution is not what you want, than you maybe will just need to add the ':id' parameter to the match code.

How to root to the current user's show view in Rails?

How can I root to the current user's show view in a Rails app?
I want to do something like
authenticated :user do
root :to => "users#show"
end
but how do I pass the current user's ID into this?
Thanks
I did a before_filter where I check if request.path == root_path and if so I redirect to the path that should be user-specific root. The root_path set in routes.rb is not user-specific root for any user so there is no infinite redirection. Just do flash.keep to make your flash messages survive the redirection.
EDIT:
Reading Q&A and comments, trying to understand what you already has, and what you still need. Did you succeed to setup routing to get show action rendered without the :id in the URL? If so maybe you need something like this in your controller show action:
if params[:id].nil? # if there is no user id in params, show current one
#user = current_user
else # if there is the user id in params just use it,
# maybe get 'authorization failed'
#user = User.find params[:id]
end
The following worked for me.
In routes.rb:
root to: 'users#current_user_home'
In users_controller.rb:
def current_user_home
redirect_to current_user
end
Is it always the 'current' user or any arbitrary user?
If it is the current user, just direct them all to the same page (without specifying the ID) and in the controller action get the current user (from session etc) and pass it through to the view.
Current user's ID should not be in url, that should be store in session. So you don't need pass it to the route.
Edit:
After reading your comment, I think you could define another action like profile for show the current user 's view.
Or at your users/show action, add some code like:
if current_user.is_admin?
#user = User.find params[:id]
else
#user = current_user
end

Rails route dependent on current user

I'd like to create a rails route for editing a user's profile.
Instead of having to use /users/:id/edit, I'd like to have a url like /edit_profile
Is it possible to create a dynamic route that turns /edit_profile into /users/{user's id}/edit, or should I do thing in a controller or?
You might want to create a separate controller for this task but you could also continue using users_controller and just check whether there is a params[:id] set:
def edit
if params[:id]
#user = User.find(params[:id])
else
#user = current_user
end
end
But you should note that /users normally routes to the index action and not show if you still have the map.resources :users route. But you could set up a differently called singular route for that:
map.resources :users
map.resource :profile, :controller => "users"
This way /users would list all the users, /users/:id would show any user and /profile would show the show the currently logged in users page. To edit you own profile you would call '/profile/edit'.
Since a route and controller serve two different purposes, you will need both.
For the controller, assuming you're storing the user id in a session, you could just have your edit method do something like:
def edit
#user = User.find(session[:user_id])
end
Then have a route that looks something like:
map.edit_profile "edit_profile", :controller => "users", :action => "edit"
This route would give you a named route called edit_profile_path
Tomas Markauskas's answer could work, but here's the answer to your question from the Rails Guide:
get 'edit_profile', to: 'users#edit'
So, when someone goes to www.yoursite.com/edit_profile, it will route to www.yoursite.com/users/edit.
Then, in your controller you can access the user with
#user = User.find(session[:current_user_id])
Assuming you set that session variable when someone logs in. Also, don't forget to check if they're logged in. This will work if your using Resourceful Routing (the Rails default) or not.
Source: http://guides.rubyonrails.org/routing.html
make the route as
get '/users/:id/edit', to: 'users#edit', as: 'edit_profile'
As explained in this link section 'The hard way' :
http://augustl.com/blog/2009/styling_rails_urls/
The url will be
/users/edit_profile
Because the ID is no longer in the URL, we have to change the code a bit.
class User < ActiveRecord::Base
before_create :create_slug
def to_param
slug
end
def create_slug
self.slug = self.title.parameterize
end
end
When a user is created, the URL friendly version of the title is stored in the database, in the slug column.
For better understanding read the link below
http://blog.teamtreehouse.com/creating-vanity-urls-in-rails
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"

Resources