3
votes

RESTful resource, default type routes. Creating an event is supposed to work as follows:

def create
    @event = current_user.events.build(params[:event])
    if @event.save
        redirect_to @event, :flash => { :success => "Event created!" }
    else
        render :action => "new" # new_event_path
    end
end

When invalid data is entered, it does render the "new" view/form again, but it renders this view at the "localhost:3000/events" URL, where the "index" action/view should be on.

My event routes seem like they ought to be pretty predictable:

    resources :events

I just updated to Capybara 2, began using DatabaseCleaner, and set transactional_fixtures to false in preparation for testing some JS-enabled functionality but can't think of any other way I might have stuffed this up.

Is there some simple thing I'm missing that could cause a weird routing muck up like this?

Ideas, anyone, on where to start troubleshooting it?

2

2 Answers

1
votes

This is the correct behavior. What is happening is that it is using the POST method for that URL when issuing the create action. Using a GET at the URL would be the index action. Also note that rendering a different template does not change the URL (that would require a redirect).

Check out section 2.2 in the Rails Routing documentation: http://guides.rubyonrails.org/routing.html

0
votes

When you create event from submitting form you using post method to /events route. And when data becomes invalid rails render events/new for your /events(POST) request at /events address.

But you can

redirect_to action: "new", event: @event.attributes  

and add to new action

@item = Item.new(params[:item].except('protected attributes','created_at', 'updated_at', 'id'))