Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:49:12+00:00 2026-05-15T21:49:12+00:00

I’m trying to get my head around this one: Say you have two models

  • 0

I’m trying to get my head around this one:

Say you have two models where:

:bar has_many :foos

And you have a url like this: http://myapp.com/24-name-of-the-bar-to-param/foos/new

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.

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”

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.

I’m running into this problem: Currently I have a login_required method in my application controller like this:

    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

This login required action is called on the create action of the foo. When I click submit on the form it takes me to http://myapp.com/foos instead of http://myapp.com/24-name-of-the-bar-to-param/foos/new

I assume this is because the login required function is called on the create action and not the new action.

Any ideas?

UPDATE as per request here is the controller code and callbacks:

  before_filter :find_bar, :except => [:index, :edit, :update]
  before_filter :login_required, :only => [:create]
  ssl_required :edit, :update


  def new
   @foo = Foo.new :amount =>  "0.00"
   @foos = Foo.find(:all, :conditions => ["bar_id = ?", @bar.id], :order => "created_at DESC").paginate :page => params[:page], :per_page => 10
   @foos_all = Foo.find(:all, :conditions => ["hatlink_id = ?", @hatlink.id], :order => "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 << @foo)
    flash[:notice] = "Thank you for fooing!"
    redirect_to new_bar_foo_path(@bar)
   else
    render :action => :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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:49:13+00:00Added an answer on May 15, 2026 at 9:49 pm

    Silly of me to come up with a solution just five minutes after posting the question. Oh well, here’s what I did (and it works).

    In the “new” action of foo I added these lines

    if !current_user
        store_location
    end
    

    In the login required method I have added this:

    if params[:controller] == "foos" && params[:action] == "create"
                    #Took out the line for storing the location in this method.
                        flash[:notice] = "Please log in"
                    redirect_to login_path and return false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.