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 4016294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:43:21+00:00 2026-05-20T09:43:21+00:00

They are two users: User – logged in user Guest – not logged in

  • 0

They are two users:

  1. User – logged in user

  2. Guest – not logged in visitor.

User and guest can post a question. To post a question, guest must specify his email.

They are two views “new.html.erb” and “new_for_guest.html.erb”. The first one relies to @user variable. The second one does’t.

When question, being created by guest, fails validation, “new_for_guest.html.erb” should be rendered, preserving all the entered data.

The code is following:

def new
  @question = Question.new

  guest = session[user_id].nil?

  respond_to do |format|
    if guest
      format.html { render "new_for_guest" }
    else
      format.html { render "new" }
    end
  end
end

def create
  @question = Question.new(params[:question])

  guest = session[user_id].nil?

  respond_to do |format|
    if @question.save
      flash[:notice] = 'Question was successfully created.'
      format.html { redirect_to(@question) }
    else
      if guest
        format.html { render :action => "new_for_guest" } # problem
      else
        format.html { render :action => "new" }
    end
  end
end

When validation fails for guest and “new_for_guest” view is rendered, i see in browser the url “/questions”, instead of “/questions/new”. Because of this, all the stylesheets, used for “new” action are gone.

When validation fails for user and “new” view is rendered, i see the correct url “/questions/new” and everything is ok.

When i just say

format.html{ redirect_to new_question }

“new” action will be rendered, but all the data, user has entered, is gone.

I need to render “new_for_guest” view in “questions/new” context.

How to do it ?

Update

I noticed the same behaviour of untouched code, generated by scaffold.

When by creation, validation is failed, new action is rendered again, but in URL, “/questions” is presented instead of “/questions/new”.

This is strange. Is this a correct behaviour ?

I’ve found similar unanswered question Rails create action is redirecting to index when it should be rendering the new action

  • 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-20T09:43:22+00:00Added an answer on May 20, 2026 at 9:43 am

    This is the correct behavior. As per RESTful routes, when a form is POSTed for an object it is sent to the /object URI, implying the creation of a new object with an unknown ID. This is described in the Rails Guides. As a result when validation fails and you simply render the new action, you are on that URI rather than the /object/new URI.

    Further, you shouldn’t have your styles be specific per action, as there will certainly be unneeded code redundancy there (and I’m not even sure how you’re doing this such that it doesn’t work when the URI changes). The same goes with having multiple views for an action, it is rarely absolutely necessary, and it doesn’t appear it is in your case since you can simply utilize an empty @user object when it’s a guest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Models - Users and Restaurants. They can set tweets like status
Or are they two distinct applications that are not related?
We have two users: user1 user2 They both belong to the group 'admin'. We
Unfortunately, my question is not as simple as keeping track of two windows created
You have regular users that use your website/services, they can login to mainsite1, then
I have an application in which users can send me feedback. When they do
These two may look like they have no correlation but bear with me! In
Here are two different questions but I think they are related. When using Git,
i have two iphone apps and want to they able to switch via a
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.

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.