Embed Devise new session form in other controllers' pages - ruby-on-rails

My Rails 5.2, Devise 4.7 app has the standard single sign_in page for new devise sessions.
I want the user to be able to browse a few different pages before signing in, and for each page to have the sign in form on it.
How can I display the devise form for new sessions in the views of other controller actions? Is there a way to run the code from the Devise::SessionsController#new action, from my other controllers? Or do I need to keep a single signin page for new sessions and then add custom things to the page via separate ajax actions?

I think you have a three ways:
You can create custom popup with same form but change form to remote and also change your form_for accordingly.
You can add ajax processing to Devise::SessionsController#new action (add respond_to and create js template to render it) and then call this action on needed pages.
Move to React/Angular/Vue

I put an empty <div id="preview_here"</div> in app/views/devise/sessions/new.html.erb and it gets populated from ajax calls (to non-Devise controllers with skip_before_action :authenticate_user!)

Related

Using custom rails admin action to render app view

I am trying to create a custom action in rails admin. I have the action setup in the sense that the icon is now present and can click on it but I am trying to figure out how to get the action to do what I want it to. Essentially I need it to render a view that I have setup in app/views/...etc. I can't seem to find any examples on how to do this and if it is even possible.
The view can be accessed outside of the rails_admin portal by going to: www.myapp.com/clerk/<some_id>. I need to action in rails_admin to take me to this page.
Most examples i see either call and API on the object or some function on the object... Any help is appreciated.

Ruby on Rails, how to stop navbar rendering on Log-In page?

In my application a user is taken straight to log-in before they can enter the main site.
I am rendering a navbar partial across the site but I don't want the navbar rendered on the log-in page. I don't want to have to copy and paste the navbar code in to all the views in order to prevent this.
It would be cleaner if there was a way I could stop the navbar showing on the login screen without having to remove the partial.
How do you restrict a partial view from one page in your application?
I am using Devise.
I don't think code is needed in this post but happy to provide if helps. Thanks.
If you are using Devise and you are in a login page, then there is no logged user. Then you may use one of the Devise helpers to achieve what you want. Something like:
if (not user_signed_in?) then
render 'your_partial_with_the_navbar'
end
The only point here is the fact the partial wouldn't render in any page where there is no user logged.
You could also create a better helper yourself, combining the user_signed_in? from Devise with other conditions of yours.

Update devise user from multiple forms

I'm working on a site where the sign-up process is multi-step.. so many little forms all update the user model (i'm using the devise gem for user authentication).
Should I post each of these steps to the same update action (overriding the RegistrationController update action)? Each form may require some logic before the update occurs, therefore the forms will require some way of figuring out what form it's currently processing (either a hidden field or trying to establish from what params exist)
..or should I be posting the forms to individual actions (which isn't very RESTful)?
Thanks!
I would not override the RegistrationsController to add an update action. I would have a completely separate UsersController that defines an update action that takes the parameters for updating a user.
Inside Spree (the thing I work on) we've got an update action on CheckoutController that works similar to how you want your users update action to work. Check it out.

Rails use different layouts?

I know rails uses the default application.html.erb. I have a profile and on my profile I have videos, photos etc, how can I separate my homepage(Home, About, Signup, Login) from the actual application? The problem I am having is when a user tries to signup rails throws an error
Couldn't find Site with subdomain =
That is because I have not created a subdomain nor site since I am unable to sign up to create one.
How can i tell rails to use application.html for pages like home, about, signup, etc and use home.hmtl.erb for the profile pages and videos etc?
create a controller called home or something similar. Then in the controller call:
layout :layout_name
you may need to create an index.html for it to not throw an error as well
You can add layout and its name when rendering action for each action where new lay out is required or u can create set layout method by default is set application and for special action load specific layout.

Devise redirect after sign up fails

Is there any easy way to disable devise sign_up page? I have forms for registration and authorization on one page so i don't need any other pages. I want to disable routes responsible for these pages, so that if a user enters users/sign_up he should't get to the sign up page but should be redirected to some other page.
You can overwrite the registrations controller and make the new action redirect somewhere. Here are some responses to a similar question that was posted on StackOverflow which explain the necessary steps.

Resources