Rails routing error: refusing to "create" re-renders the form (as follows) but not at the url in my routes
RESTful resource, default routes. Event creation should work like this:
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 renders the "new" view / form again, but maps that view to the url "localhost: 3000 / events" where the action / view "index" should be enabled.
My event routes look like they should be pretty predictable:
resources :events
I just upgraded to Capybara 2, started using DatabaseCleaner and set transactional_fixtures to false in preparation for testing some JS-enabled functionality, but I can't think of any other way I could fill this.
Is there some simple thing I missed that could cause weird routing?
Ideas, anyone, where to start troubleshooting?
source to share
This is the correct behavior. What happens is that when the create action is released, the POST method is used for that url. Using GET in the url will be the index action. Also note that rendering another template does not change the URL (which requires a redirect).
Check out section 2.2 in the Rails routing documentation: http://guides.rubyonrails.org/routing.html
source to share
When creating an event from form submission, you use the post / to / events method. And when the data becomes invalid, rails display events / new for your request / event (POST) at / events.
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'))
source to share