Redirecting to a route alias for a form validation error

If I am in a route alias, for example / register, and I have a form error, and I visualize: new, is it possible for the path to be / registered yet?

This is currently rendering / new

I could do redirect_to register_path, but then I would lose the parameters?

This makes the following test fail:

  Scenario: Try registering with a bad staff number
Given I am on the registration page
When I fill in "email" with "kevin@acme.com"
And I fill in "First Name" with "Kevin"
And I fill in "last name" with "Monk"
And I fill in "Employee Number" with "something barking123"
And I press "Register"
Then I should be on the registration page
And I should see "Your employee ID number looks incorrect."
+5
source share
5 answers

An alternative would be to create two “register” routes. One that accepts only GET requests (and jumps to: the new action), and another that accepts only POST requests (and jumps to the create action, but displays the same template as the new one).

It might look like this:

map.get_register 'register', :controller => :registrations, 
                 :action => :new, :conditions => { :method => :get }
map.post_register 'register', :controller => :registrations, 
                 :action => :create, :conditions => { :method => :post }

:

def new
   @registration = Registration.new
   # renders the 'registrations/new' template
end
def create
   @registration = Registration.new(params[:registration])

   # render the 'registrations/new' template if we fail validation
   return render(:action => :new) unless @registration.save
   # otherwise renders the "create" template which is likely a thank-you
end

, :

+1

Rails (, ), , , . , , . , , .

+1

, URL- , GET'd POST'd. , URL-/, , "Register" POST'ing /new. , , " ", " " , , -.

+1

, "create" "" "update" "". .

, . , URL- "" - . / URL-, GET.

, , , . , .

: ..

def new
  if request.get?
    @user = User.new

  elsif request.post?
    @user = User.new(params[:user])
    if @user.save
      redirect_to users_path
    else
      render :new
    end
  end
end

.

0

, . ( /register?).

, params var.

0

All Articles