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

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.

Related

Embed Devise new session form in other controllers' pages

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!)

How to work with tabbed signup/signin form using devise in rails

I am a new to the whole rails environment and learning to implement 'devise' gem for my project. Currently I have a tabbed signup/signin form.
I have generated the devise views and modified the /app/views/devise/sessions/new.html.erb to show this kind of tabbed form.Now functionality wise everything working fine. But there are several issues.
When I am hitting http://localhost:3000/users/sign_up it still shows the devise old signup form. I want to show the same form with the Registration tab activated.
On the Register tab if I submit the form with some error (Empty Email/Password), it is again redirecting to the default device registration form to show up the error messages.
I just want to get rid of this default registration page and want to use my Register tab for all signup purposes.
This question may sounds very basic, but I have no idea how to customize it.
Please help.
Well, I like to make reference to the comment from #max as an answer, so that others could take that as an answer to this question.
I believe you are getting those errors because you are hacking your session views in Devise. The registration view in Devise is at /app/views/devise/registrations/new.html.erb. You have to refactor your code to do away with the errors.
Answer is courtesy of #max

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.

How to Customize Registration using Devise on Rails?

I'm currently working on a Rails project and have decided to use Devise for user registration. The site is using MongoDB (mongoid gem), and I am planning to create a simple sign-up/sign-in system.
So, there is a link in the home page that allows the user to click on it or open in another tab. If he decides to click it, there should be a popup modal that contains the fields and sign-up button, etc. If he opens a new tab, he should see a dedicated page for the sign up process.
So, here is what I have so far. I have installed devise and am able to sign up properly. However, I also want to create the modal effect, in which I used jQuery UI dialog attached to the sign-up link. Then I loaded the sign-up page (/user/sign-up) using dialog.load("path") and stripped the layout when it is an ajax request.
I know this is not the best method to use, so I've been reluctant. Is there a better way of doing this? Preferably a standard method. Any help would be appreciated, or just point me to the right direction. Tutorials will be very nice and helpful. Thanks a lot in advance!
I am not sure if I understand the question, but try this. Run
rails generate devise:views
To create the devise views.
In the views/devise/registration folder you will find the registration page. You should now have access to both the form code and the path user_session mapping to /users/sign_in.
You now have the form and the path, so you should be able to play with the AJAXifying it.

To implement Reddit's login feature in Rails, would I need to use RJS and remote_form_for?

A feature in Reddit that I like is the ajax login - you enter your username and password on the front page and you never leave the front page even if there is a login error. If your login succeeds, I think it simply does something like a flash[:message] onto the front page to say "login successful". If I wanted to do this in Rails, would that be a case of needing to use RJS + remote_form_for? Are there any other techniques in Rails that would allow me to do this?
RJS and remote_form_for are the Rails Way to do Ajax, but you certainly don't have to use them. Railscasts has a screencast on using RJS and Ajax to submit a form.
If you prefer a different javascript library (such as jQuery), you can use its Ajax libraries to perform the correct actions.

Resources