简体   繁体   中英

Ruby on Rails, load a page in application even though user is not signed in

I'm using devise. When a user 'Signs Up' they go to a page where they enter their name, email, password etc, and then click a 'Sign Up' button and, should all details entered be correct, they proceed into the application, which is what should happen.

What I want to do is add a link, 'By clicking "Create Account" you confirm that you accept the Terms and Conditions.' on the Sign Up page. That's all it is, a simple link - no check boxes or anything.

On the 'Sign Up' page code I have:

            <div class='t_and_c'>

    By clicking "Create Account" you confirm that you accept 
the <%= link_to "Terms and Conditions.", "static_pages#terms_and_conditions" %>
            </div>

But whenever a person clicks the link it keeps reloading the 'Sign Up' page - which is what it should do, because some code somewhere is telling the app not to load anything until the user has signed in so my question is:

How, just for this link, 'Terms and Conditions', can I load my terms_and_conditions.html.erb, even if the user is not sign in? I suppos it's some before_filter or skip_before filter but I don't know how to go about it.

Check your StaticPagesController to see if there is any before_filter , which may be some authentication method to prevent vistors' reading unless signed in.

If that is true, just add an exclude option on the "terms_and_conditions" method, like

before_filter :authorize, :except => :terms_and_conditions

Or

before_filter :authorize, :except => [:terms_and_conditions, :other_method_as_well]

Add

As per OP's update, there is before_filter but the filter is in the parent class ApplicationController .

In this case you can't simply add another before_filter in child class trying to overwrite it. Doing this will add one more filter in filter chain.

The solution is to use skip_before_filter like this

StaticPagesController < ApplicationController
  skip_before_filter :authenticate_user!, only: :terms_and_conditions

Method reference: http://apidock.com/rails/AbstractController/Callbacks/ClassMethods/skip_before_filter

Sorted! My app ensures everybody has to sign in to get by. Unless it is a 'Terms and Conditions' link they click, on the 'Sign Up' page - which is how I want it to be.

First of all, I got my routes.rb sorted out. Nothing would have worked if I didn't do that.

Instead of:

    By clicking "Create Account" you confirm that you accept 
the <%= link_to "Terms and Conditions.", "static_pages#terms_and_conditions" %>

I put:

 By clicking "Create Account" you confirm that you accept 
the <%= link_to "Terms and Conditions.", t_and_c_path %>

and in my routes.rb:

 get "t_and_c", :to => "static_pages#terms_and_conditions"

In my ApplicationController at the very top I put:

before_filter :authenticate_user!, :except => :terms_and_conditions

But it wouldn't work; the link 'Terms and Conditions' kept going again to the Sign Up page, telling me I had to sign up.

So the kind poster above said try in my StaticPagesController:

StaticPagesController < ApplicationController
  skip_before_filter :authenticate_user!, only: :terms_and_conditions

So now, at least now I was getting an error message! - a syntax error. I don't know if it's a Ruby 1.8.7 thing (which I have) but I changed the code slightly to:

skip_before_filter :authenticate_user!, :only => :terms_and_conditions

And now everything is working just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM