I have a user that signs up and logs in.
Right now their route is rails standard "user/3".
A user belongs to an organization. An org has many users.
I want all users for that org, when they sign in, to have the url http://mysite.com/:organization name.
How would I accomplish this?
I'm not sure how your authentication is setup or what you're using to handle your authentication, but presumably when a user logs in successfully you just change the redirection in your log in action. Something like:
redirect_to user.organization
instead of redirect_to user
You need to browse on nested resources. Please look at http://guides.rubyonrails.org/routing.html#nested-resources
UPDATE: more appropriate answer
I think this is more for your scenario http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html#method-i-scope
You have to do something like this...
First add a method to your organization model to show name instead of id in url
def to_param
name
end
and update your routes as required, and functionality also to show the users of that organization
Organization Controller..
def show
#organization = Organization.find(params[:organization])
#users = #organization.users
end
Redirect user to user organization show path after login.
Related
I'm running a rails 4 app using Omniauth with Twitter. I'm trying to achieve something close to producthunt.com, where they authenticate users and use their Twitter username as their url id.
From what I understand, you want the url to look like this: example.com/users/username
instead of example.com/users/123
If so, all you have to do is change the way you find the de correct user in your Users (or whatever you call your user model) controller. Currently it probably looks like this:
#Users Controller
def show
#user = User.find(params[:id])
end
# Your path to that user is probably this:
user_path(123) #basically you pass the user.id
The code above is using Model.find(#) to look for the user. The .find() looks the user up by its id#. Instead, you want to find it by the username, not id. To do this use the Model.find_by You can see all the ways of querying here.
Also, whenever you look for the path to find the user show page, istead of sending the id # to the url, you now have to send the username string.
Your new setup should look like this:
#Users Controller
def show
#user = User.find_by :username params[:id]
#this assumes you have it in your DB as username. Some twitter apps save it as screen_name.
end
# Your path to that user is probably this:
user_path('username') #basically you pass the username instead. current_user.username? I dont know what you call in in your app.
Hope that helps. Let me know if you have questions.
I can have two types of users sign up on my app, "girls" and "boys". If a girl signs up I want to redirect to "/girls" and if a boy signs up I want to redirect to "/boys".
Is it possible to do custom redirection with Devise?
The closest docs I found are here: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in. The problem is I can't do any check to switch the redirect route.
Options I've considered:
Pass an additional URL param when user clicks "sign-up". like
?is_girl=1.
After they click sign-up, when determining the redirect route, I could look at the users model and see if they're a girl or boy. Then redirect accordingly.
I am going to assume as part of the sign up process you ask them if they are a boy or girl and this is saved in the database.
So you would just need to do like the example in the Devise docs is showing
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
if resource.sex == 'boy'
'/boy' # or you could create a route in routes.rb and do boy_path
else
'/girl' # with routes setup: girl_path
end
end
So I have a polymorphic controller in rails, and my question is, is there any way for me to get the scope of the requester. for example, the rout is /users/1/messages, the request goes to the MessagesController, in that controller, is there any way to get the users portion of the route? or in this case it could also be admin or something else.
I cant just set up an if statement structure, because in this case, `POST '/users/1/messages' is a message going to a user, perhaps from an admin.
normally you should have put a type field (e.g. messagable_type), a message instance has the corresponding class (e.g. User) and the id field (e.g. messagable_id) will have the point to the id of the user. so you can ask a message instance:
message.messagable
=> user
it will return the user instance.
If you have a url such as: /users/1/messages and you want use this:
def load_messagable
resource, id = request.path.split('/')[1,2]
#messagable = resource.singularize.classify.constantize.find(id)
end
This is the solution given by Ryan Bates in this episode: http://railscasts.com/episodes/154
I have two tables: workers_table and admins_table.
I have two controllers and I use devise (sign_in, sign_up...).
I use:
before_filter :authenticate_user!
in the controllers of workers and admins.
assume I have the email of the user that signed in:
#email = current_user.email
I want to check where #email is found:
if it's found in the workers table, so redirect him to the index.html.erb of workers (and he can't enter to index.html.erb of the admins).
if it's found in the admins table, so redirect him to the index.html.erb of admins (and he can't enter to index.html.erb of the workers).
I know how to check if the user belongs to workers_table or admins_table.
But where should I check which table the user belongs to? should I define another controller?
Please guide me.
application_controller.rb would be a good place to define the authenticate_user functionality
another place could be a sessions_controller.rb if you have it
at the very least you could have an ensure_admin method for your admins_controller to add to the before_filter, and if they are not an admine, you could redirect_to workers#index
I'm new to rails, so i'm unsure as to the conventional way to do this, but the above is how I might do it.
I'm new to Rails and indeed to web development. I'm trying to do a cross-domain post (I think) and have no clue how to do it.
I have a rails app running on webrick, let's call this 'myapp'.
I have written a bookmarklet which when selected should grab the URL from whatever website the user is on and post it to 'myapp' to be saved for that user (who will need to give his email address). How would I write a controller to deal with this?
It's hard to be specific with the amount of info you've provided, but general, you'll need to set up a route to handle the request, and define a controller action to do what you want with it.
Assuming the requests look something like POST http://myapp.com/bookmarks/create with parameters for the user's email and the url they're on, that means doing something like this:
in routes.rb:
resources :bookmarks
in bookmarks_controller.rb:
def create
if params[:email]
#user = User.find_by_email(params[:email])
if #user
#user.bookmarks.create!(:url => params[:url]
end
end
end