Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: redirect two pages back
    primarykey
    data
    text
    <p>I'm trying to get my head around this one:</p> <p>Say you have two models where:</p> <p>:bar has_many :foos</p> <p>And you have a url like this: <a href="http://myapp.com/24-name-of-the-bar-to-param/foos/new" rel="noreferrer">http://myapp.com/24-name-of-the-bar-to-param/foos/new</a></p> <p>On my site this page shows a lot of information about the bar which users are going to create a foo for. So, even if a user isn't logged in the user will still be able to see the info.</p> <p>Currently, when the user is logged in, a form to create a new foo is on the left hand side of the web page. When the user isn't logged in it says "Please login or register"</p> <p>The form explains a lot about how my app works, so I'd like to change it so that even if a user isn't logged in the form will display, and if they click submit it will take them to the login_path and then when they login, back to the path where the submitted the form.</p> <p>I'm running into this problem: Currently I have a login_required method in my application controller like this:</p> <pre><code> def store_location session[:return_to] = request.request_uri end def login_required unless current_user || admin? store_location flash[:notice] = "Please log in" redirect_to login_path and return false end end </code></pre> <p>This login required action is called on the create action of the foo. When I click submit on the form it takes me to <a href="http://myapp.com/foos" rel="noreferrer">http://myapp.com/foos</a> instead of <a href="http://myapp.com/24-name-of-the-bar-to-param/foos/new" rel="noreferrer">http://myapp.com/24-name-of-the-bar-to-param/foos/new</a></p> <p>I assume this is because the login required function is called on the create action and not the new action.</p> <p>Any ideas?</p> <p>UPDATE as per request here is the controller code and callbacks:</p> <pre><code> before_filter :find_bar, :except =&gt; [:index, :edit, :update] before_filter :login_required, :only =&gt; [:create] ssl_required :edit, :update def new @foo = Foo.new :amount =&gt; "0.00" @foos = Foo.find(:all, :conditions =&gt; ["bar_id = ?", @bar.id], :order =&gt; "created_at DESC").paginate :page =&gt; params[:page], :per_page =&gt; 10 @foos_all = Foo.find(:all, :conditions =&gt; ["hatlink_id = ?", @hatlink.id], :order =&gt; "created_at DESC") @current_user = current_user @topfooers = User.bar_amount(@bar, nil) @average_foo = @bar.foos.average('amount') end def create @foo = @current_user.foos.build params[:foo] if (@bar.foos &lt;&lt; @foo) flash[:notice] = "Thank you for fooing!" redirect_to new_bar_foo_path(@bar) else render :action =&gt; :new end end private def find_bar @bar_id = params[:bar_id] return(redirect_to(categories_path)) unless @bar_id @bar = Bar.find(@bar_id) end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload