Change the url based on current_user - ROR - ruby-on-rails

Say for instance I have a posts controller that currently has a method user_posts which shows all of the posts that are associated with the user with the associated id as so:
def user_posts
#user = User.find(params[:id])
#posts = #user.posts.all
end
I want the url to be: foo.com/my_posts when the posts have the same ID as my current_user; How would I do this? currently my routes are set up as so:
get 'user/posts/:id', to: 'posts#user_posts', as: 'user/posts'
I know that I could create an entirely new controller action for my_posts but I want to know if there is a way to do it in the config/routes.
If for example I am browsing throughout the site and tap on a link that says "user posts" I would expect to go the the users posts and if that user happens to be me I would like the url to show website.com/my_posts

If I understand well, you have a list of users (including the currently connected user) and each has a link 'user posts' to see the user's posts.
You can simply do:
views
In your views, change the user post link according to the user id. As you loop through your users, check if the user's id is the same as the currently logged user. If yes, change the link to the /my_posts route as follow:
<% if user.id == current_user.id %>
<%= link_to "My posts", my_posts_path %>
<% else %>
<%= link_to "User posts", user_posts_path(user) %>
<% end %>
routes.rb
Add a my_posts route that points to the same controller method as user/posts.
get 'user/posts/:id', to: 'posts#user_posts', as: 'user/posts'
get 'my_posts', to: 'posts#user_posts', as: 'my_posts'
controller
In your controller method, we need to instantiate the #user to get its posts. If there is no :id in the params (like the /my_posts route), then set the #user to the current_user. If an :id is passed, set the #user by fetching it from the db.
def user_posts
#user = params[:id].present? ? User.find(params[:id]) : current_user
#posts = #user.posts.all
end
No need to do checking in the routes.rb file. This is simple and more "Rails" like.
Is this what you are looking for?

As I know - no. It's possible to create in routes redirect route and check some conditions (example from documantation):
get 'jokes/:number', to: redirect { |params, request|
path = (params[:number].to_i.even? ? "wheres-the-beef" : "i-love-lamp")
"http://#{request.host_with_port}/#{path}"
}
But it's impossible to check current user in routes. Redirect can be implemented in the controller with two separate actions as mentioned.
Also available a little trick - generate from the beginning 'right' routes if you use html.erb (slim/haml). For current user posts link can be generated not as usual user/posts/:id but /my_posts (it's possible to check current user id without any problems) and define two routes:
get 'user/posts/:id', to: 'posts#user_posts', as: 'user/posts'
get 'my_posts', to: 'posts#user_posts', as: 'my_posts'
In controller check request.path to find user:
user = request.path == '/my_posts' ? current_user : User.find(params[:id])
Hope it helps.

I'm guessing you didn't want to use the index method of the posts controller because you were using it to show all posts from all users, but you can still use it. Here's how:
class PostsContoller < ApplicationController
def index
#posts = if params[:user_id].present?
User.find(params[:user_id]).posts
else
Post.all
end
end
end
Then in your routes file do this:
resources :posts
resources :users do
resources :posts
end
This allows posts to be a first class resource as well as a nested resource. Now when you go to /posts/ you get all posts, but when going to /users/:user_id/posts you get only posts from the given user.
In your app, when you need to link to all posts from all users, you can do
posts_path
and when you need to link to just a user's posts you can do
user_posts_path(user)

Related

rails Couldn't find User with 'id'=

When I try to load the user's show view, it get the error Couldn't find User with 'id'=
it specifically points to the second line in the UsersController:
def show
#user = User.find(params[:id]) ## shows the error in this line
end
The funny thing is, it USED to load, up until I changed the routes to this:
Rails.application.routes.draw do
devise_for :users
get 'users/show'
root 'users#show'
get 'welcome/index'
resources :users
end
Why am I getting this error after changing the routes?(which I changed, because my objective is to go straight to the users profile after devise log in)How can it be fixed?
Oh, and the following is my user show view in case the error is in there:
<h1>This is the user show view </h1>
<p>Your email is: <%= #user.email %></p>
<p>Your name is: <%= #user.name %></p>
Thank you.
This does not work:
root 'users#show'
It tells rails to use your users#show page as startpage
But rails does not know which user it should show then. There is no user id given then. That is why you see in the error message id= without something behind the equal sign.
If you want to go to show action after login, instead of
#user = User.find(params[:id])
you can do:
#user = current_user
Devise by default redirects you to the root path.
The default root path 'welcome/index' is a collection action. That is, it does not need an id to be passed in.
Now you have changed that to a member action 'users/show/:id'. So you need to pass in the id.
The simplest way is to override devise in application controller like this:
def after_sign_in_path_for(resource)
redirect_to root_path(current_user.id)
end

Add new route example.com/username while preserving existing routes

Is there a way to have example.com/username while preserving example.com/users/1 route?
To put simply, I just need to link to users's profile via username.
You can add an arbitrary route like this using:
get "/:username" => "users#show", as: :username
This will pass the username as a parameter to the show action.
Then in your views:
<%= link_to user.name, username_path(username: user.username) %>
You'll also need a controller action that knows how to handle the username parameter. If you want to use your existing users controller and preserve your /users/:id URLs too, you could do something like:
def show
#user = if params[:username].present?
User.find_by_username(params[:username])
else
User.find(params[:id])
end
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