Rails3: Upon Devise log in, route to document identified in database - ruby-on-rails-3

My Rails3 app uses Devise for authentication.
Just after clicking the "log in" button, I want each user to be routed to a given document (id of this document is stored in database, changes often).
Is there a best-practice to handle this in Rails3?
I see two options:
Create a single huge page, which checks the database and renders the appropriate document accordingly.
In app/controllers/application_controller.rb, check the database and route accordingly to the right document.
Which is best? Any smarter idea?

You could override the after_sign_in_path_for hook in your login controller (or application controller) to be something like:
def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User)
#Whatever url you need to here
else
super
end
end
and that should do the trick.

Related

Accessing a url directly without login

I am considering doing this -
Any url (excecpt those I disallow specifically) can be accessed directly without signing-in, however if you click on any of the links on the page, it will redirect you to the sign-up page
I am thinking of several ways of doing it, but neither is flexible enough to work with devise
Create a new link_to_not_registered helper which I will use on every link_to and it will check if the user is logged in or not
create a before_filter to check if the user is logged in. This is a bit problematic, as I don't know how to create a filter only when linking and not when directly accessing a page
Have an external flag to test if the user is logged in and change the page accordingly.
neither way helps me redirect the user after sign-in/sign up (new helper links to sign up, before filter becomes too complex, flags are too simple)
Is there a way to create a functionality of direct access to show actions while clicking on links requires login?
I think the best approach is a before_filter. You can check previous page by request.referrer, so if it's a page inside your app, you redirect user to signin path
def to_signin
redirect_to singin_path if request.referrer["http://myapp.com"]
end

How to remove the active admin sign-up link?

I don't want to sign-up new user to (active admin) admin panel..so that I want to customize the login page of active admin.
How can I remove the sign-up link from the admin-login page in active admin.
How can I do the same...?
The question is quite old, but I just came across the same problem. My solution is:
mkdir -p app/views/active_admin/devise/shared
touch app/views/active_admin/devise/shared/_links.erb
I have also disabled the routes:
devise_for :users, ActiveAdmin::Devise.config.merge(skip: [:confirmations, :passwords, :registrations, :unlocks])
If this rule applies to all of your admin pages, you could use a different layout file that didn't include the links (or the partial that included them.
You could set a variable in the controller (e.g. #hide_login) then conditionally display them (e.g. <%= link_to("Sign Up", sign_up_path) unless #hide_login %>)
I have worked on a number of applications where the admin interface is really a separate part of the app, accessible only to internal users, and in this case it can be helpful to put your administrative models/views/controllers in their own namespace ( e.g. Admin::ManageUsers) which makes it easy to globally apply certain rules in a before_filter (including, possibly defining the default layout).
There are several posibilities to do this as you know you should have a controller (I mostly use AdminController) wich has an index action.
then in de index view there probably is a render partial wich renders the login/sign-up form
you can locate the elemement wich renders the sign-up link.
If you somehow can't find this you can go to your Terminal/CMD
end type
grep -lr "sign-up" *
this will find the sign-up link somewhere then just delete it or hide it like above message suggests

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

collect email using hominid/mailchimp rails 3.0

I would like to collect emails of prospective users of my app. I've created a page with a simple form. My first question is that since I'm not using a db, do I need a model? And secondly, how do I use "form_for" for form generation if I don't need to use a model. Thank you
I've done this. First I created a simple form with an email field and submit button in my views (app/views/home.html.erb). I use form tag helper to easily create the form rather than writing my own html: http://guides.rubyonrails.org/form_helpers.html. I made sure route the form action to the correct action in my controller. In the controller I use hominid to do the subscription, then redirect to the index using redirect_to (':action => home'). That's all it took.

Customizing devise layouts for registrations vs. passwords

I have an application that uses devise for authentication. I am trying to customize the layouts for the devise controller, and I was able to do some of this by following the answers to another question here on Stack Overflow. However, I can't find anything about how to distinguish between the devise/passwords and devise/registrations controller. I am using the following code:
def layout_by_resource
if devise_controller?
if action_name == "edit" or action_name == "update"
"application"
else
"sessions"
end
else
"application"
end
end
The problem is that when a user tries to re-set their password, it's trying to use the application layout (since the action is edit). I need it to use the sessions layout. Can anyone help me figure out how to make that happen?
You should look at this response on different layout for sign_in action in devise, which highlights the Devise docs on How To Create Custom Layouts