Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Address bar URL gets changed on Validation failure in Rails


  • Please log in to reply
1 reply to this topic

#1 Ajay149

Ajay149

    Passenger

  • Members
  • 1 posts

Posted 16 March 2014 - 05:24 AM


I just overridden views/spree/user_registations/new.html.erb and added my contents in this file.

when I hit localhost:3000/ i get my custom form.

On validation failure case it's rendering the action 'new'.

Next, after validation failure the 'new' action section gets rendered & I get the view.

But in the browser address, I get "http://localhost:3000/spree/signup".

I want it to be localhost:3000/ only.

How can we achieve this and see only localhost:3000/ in address bar on validation failure ?

#routes.rb :
root to: 'spree/user_registrations#new', as: '/'

This is my View Section :

#spree/user_registrations/new.html.erb

<%= form_for @user, url: spree.registration_path(@user), id: 'user_new' do |f| %>

<span> <%= f.text_field :email, :placeholder => "Email *", name: 'email' %>
<span> <%= f.password_field :password, name: 'password' %></span>
<span> <%= f.password_field :password_confirmation name: 'password_confirmation' %></span>

<% end %>

Controller :

Spree::UserRegistrationsController.class_eval do
def new
@user = Spree::User.new
end

def create
@user = Spree::User.new(email: params[:email], password: params[:password], password_confirmation: params[:password_confirmation])

if @user.save
session[:customer_id] = @user.id
set_current_user(@user.id)
redirect_to main_app.profile_dashboards_path and return
else
render 'new'
end
end
end

end

How the URL will remain unchanged (localhost:3000/) after validation failure ?

#2 himanshu339

himanshu339

    Passenger

  • Members
  • 9 posts

Posted 17 March 2014 - 07:25 AM

instead of "render 'new'" in controllers create method, use redirect_to and root path. In this way it will be redirected to same page in case of any errors.

 

I hope this helps!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users