Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Form not going to create action
7 Posts
Form not going to create action

I'm having a problem when submitting my form. The problem I'm facing is that the form action is going to the index action and not to the create action.

Here is my code : http://pastie.org/379895

It worked before but now it isn't and I didn't change a thing to my code of the new and create action.

Does anyone know what the problem may be?

Let me start by saying - great post. Good pastie, clear question, a pleasure to diagnose :)

This line in your routes is causing your problems:

map.nederlands_index 'nederlands', :controller => "nederlands", :action => "index"

What that's doing is mapping ALL methods for /nederlands to the index action, both GET and POST. Now think about what Rails's RESTful routes does/expects. A GET to /nederlands is sent to the index action, and a POST to /nederlands is sent to the create action. So you've overridden these with your named route sending all methods for /nederlands to the index action.

You can do one of three things:

# Just remove that route. You don't need it because .resources already gives you named helpers for your RESTful routes, it gives nederlands_path and nederlands_url for the index action. # Move the route after your map.resources so that it doesn't interfere. # Add a condition for this nederlands_index route so that it only matches for the GET method:

.

map.nederlands_index 'nederlands', :controller => 'nederlands', :action => 'index', :conditions => { :method => :get }

Thanks for your help jason, that was indeed the problem. I'll adapt my routes file so it will not interfere with other actions.

You're welcome.

hmmmm....I think y'all just showed me how to force all invalid requests on a controller to go to one specific action...

What would happen if the route entry:

map.nederlands_index 'nederlands', :controller => "nederlands", :action => "index"

was moved to the bottom of the nederlands pile? Would the router try all of the above and "fall-through" to this and thus send you to the index action for "everything else"?

I can't see of any way to make that work. map.resources is too powerful, it maps all resources to itself, not just the RESTful routes, and then even if you use the :only clause to limit what it maps, it's going to see /nederlands/crappy to the :show action using "crappy" as the id attribute.

Better to handle this through the standard method of catching the exceptions and redirecting.

I've created in my controller this rescue, and I have to say it works fine :

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found rescue_from ActionController::UnknownAction, :with => :record_not_found

7 Posts
Login to add your message