When a form fails, the display URL changes in Rails 4

I created a user registration page. When a user submits a form incorrectly, when a validation error is displayed, it does not display at the same URL.

The registration form is located at this URL:

http://localhost:3000/signup

I added the following routes for the registration page:

resources :users
match '/signup',  to: 'users#new', via: 'get'

When I submit the form, a model confirmation appears, but the URL is redirected to:

http://localhost:3000/users

I want the user to stay at the same URL after submitting the form.

http://localhost:3000/signup

This is my controller code:

def new
    @user = User.new
  end

  def create

    @user = User.new(user_params) # Not the final implementation!

    if @user.save
      # Handle a successful save.
    else
        render 'new'
    end
  end

This is the start of my form tag:

<%= form_for @user, url: {action: "create"}  do |f| %>
+4
source share
1 answer

Rails 5, , - .

routes.rb:

get 'signup', to: 'users#new'
post 'signup', to: 'users#create'
resources :users

:

<%= form_for @user, url: signup_path  do |f| %>

, , , , , ( 4.2 ) url: {action: "create"}, , resources :users, post '/users', to: 'users#create (rails 4.2 ). , route.rb, , http://localhost:3000/users.

, , , , , route.rb .

+2

Source: https://habr.com/ru/post/1540547/


All Articles