Github like dynamic routes - ruby-on-rails-3

What would be the best way to implement routes like github uses?
Ex:
github.com/about
github.com/37signals
github.com/javan
I'm guessing /about is a real controller, but the second and third probably load a user controller. What is the best way to do this?

Write a route like match '/:id/' => 'user#show' for a user like javan and have the show action in the user controller look up the user by username.
So in the user controller:
#user = User.find_by_username(params[:id])
You can learn more about routes here.

Related

Manage route in yii by users

I have one task and I really don't know how to do that.
So there is the table with all actions in Yii web application. All authorization's users can create their own link that indicate to particularly action, for example: There is action name's actionGetAllMovies, and user Sarah set her link 'favouriteMovie', Jack set like 'coolMovie',etc... and when they type their link, they redirect to action actionGetAllMovies.
How did "teach" YII realized properly reads this links?
in parent controller, in BeforeAction you can get:
1) Controller name: Yii::app()->controller->id
2) And action name: Yii::app()->controller->action->id
3) As you normally known user_id: Yii::app()->user->id
We can only make a request to the database is there for user, controller and action redirect.
Or maybe I misunderstood something?

rails devise after first sign in path

Is there a way for us to configure devise to go to a specific page after sign up? Something like after_sign_up_path for. I want the users to update their profile on first login after signup, so want to redirect them to edit_user_path(current_user).
Got it. Below is how i implemented it.
scope = Devise::Mapping.find_scope!(user)
sign_in(scope, user, {})
redirect_to edit_user_path(current_user)
Not sure if you know it already
But there is a after_sign_up_path_for method which you could override now
https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L98

ruby on rails 3, render multiple views in one view

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

Rails app - Wanting something more than CRUD

I have a rails app where a User controller has all the CRUD methods new create destroy update however, I want an action now that does something more.
When the user clicks a button, I want to perform some logic and then forward them to their accounts page. The logic will go in the new method I make inside the controller. However, I cant find out how to make a form that submits to this new method I make in the User controller.
I'm trying something like this:
form_tag "/users"
but how do I make this form execute the new method I've made in the User controller
def some_logic
....
end
Update
After some reading, Is this the best way to go?
routes.rb
match '/download' => "users#some_logic"
view
= form_tag "/download"
This will execute the some_logic method
I tend to do in the view:
form_tag :action => :some_logic do
and in the routes you can have:
post "/download", :to => "users#some_logic"
That should make a form which sends the user off to the some_logic action within
UsersController
Take a look at the docs for further restful actions. You can find it here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Rails 3 - Routing to a user profile

Greetings all, newbie to Rails here. I'm currently having issues routing /profile to the current user's profile.
My route is as follows:
match "profile" => "users#show"
However, this is hitting me with the "Couldn't find User without an ID" error. I know it has to do with my show method in the Users Controller. That code is simply:
def show
#user = User.find(params[:id])
end
Now, I could add another method in my Users controller with "#user = current_user" and it works fine. However, it seems a bit redundant and would also require a copy of the show view page. From what I've gathered with Rails, it's all about keeping things neat and tidy.
I would appreciate it if someone could point me in the right direction. Thank you.
RailsGuides states:
Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.
So I think you want to change your code to be the following
def show
#user = !params[:id].nil? ? User.find(params[:id]) : current_user
end