Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any way to avoid having the browser's address changed after using "render"?
    primarykey
    data
    text
    <p>I am currently following the <a href="http://ruby.railstutorial.org/" rel="nofollow">Ruby on Rails Tutorial by Michael Hartl</a>. And there is something that has been bugging me for quite some time. I looked it up but I still can't find a good answer.</p> <p>Anyway, I've noticed is when you have a validation error in the signup page it renders the original signup page and changes the nav bar address. I've matched <strong>/signup</strong> to the action <strong>new</strong>, but if I use <strong>render</strong> it changes from <strong>/signup</strong> to <strong>/users</strong> (the default, because of the RESTful standard I guess). </p> <p>I'll leave some lines of my code:</p> <p>routes.rb</p> <pre><code> resources :users match '/signup', :to =&gt; 'users#new' </code></pre> <p>users_controller.rb</p> <pre><code> def new @user = User.new @title = "Sign up" end def create @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = "Welcome to the Sample App!" redirect_to user_path(@user) else @title = "Sign up" @user.password = "" @user.password_confirmation = "" render 'new' end end </code></pre> <p>So I've tried to work around this by not using the <strong>render</strong> method but <strong>redirect_to</strong> instead but I'm having trouble using it. As it is actually sending data to the path provided, @user.errors gets overwritten by creating a new instance of the model and the flash variable cannot show the errors.</p> <p>_errors.html.erb</p> <pre><code>&lt;% if @user.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt; &lt;%= pluralize(@user.errors.count, "error") %&gt; prohibited the user from being saved: &lt;/h2&gt; &lt;p&gt;There were problems with the following fields:&lt;/p&gt; &lt;ul&gt; &lt;% @user.errors.full_messages.each do |message| %&gt; &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>My question is: is there any way that by using <strong>render</strong> I can change the url displayed on the navbar? It's really frustrating if someone makes a mistake in the signup form, presses enter in the navbar and ends up in a totally different place.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    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