views:

142

answers:

3

I'm trying to edit a form, the route is controller/id/action for edit so for example

people/124321/edit

I'm trying to make this form submit to the update action using this code:

<% form_for :probe, @probe, :action => "update" do |f| %>
   ...
   ...
   ...
   <%= submit_tag 'Submit' %>
<% end %>

When I click submit, it gives me an error stating

Unknown Action. No action responded to (id).

Edit

The only thing in my routes specified for probes is map.resources :probes

RoR just did the people/124321/edit by itself when I generated the controller.

Rake routes shows this

probes       GET /probes(.:format) {:controller=>"probes", :action=>"index"}
             POST   /probes(.:format) {:controller=>"probes", :action=>"create"}
new_probe    GET    /probes/new(.:format) {:controller=>"probes", :action=>"new"}
edit_probe   GET    /probes/:id/edit(.:format) {:controller=>"probes",action=>"edit"}
             GET    /probes/:id(.:format) {:controller=>"probes", :action=>"show"}
             PUT    /probes/:id(.:format) {:controller=>"probes", :action=>"update"}
             DELETE /probes/:id(.:format) {:controller=>"probes", :action=>"destroy"}

Edit 2 Probe Controller

    def edit
     @probe = Probe.find(params[:id])
    end

    def update
    @probe = Probe.find(params[:id])
    debugger
    if @probe.update_attributes(params[:probe])
      flash[:notice] = "Successfully updated probe."
      redirect_to probes_path
    else
      render :action => 'edit'
    end
  end
+3  A: 

It's hard to say exactly since you have posted very little supporting details for your question, but my guess is that your routes file is set up such that the precedence of something matching :controller/:action/:id comes before the route you're aiming for, :controller/:id/:action.

Routes are evaluated top-down, first match wins.

I'll echo John's answer, too. You shouldn't need to specify :action => 'update', and in fact these days I usually extract the form out of both new.html.erb and edit.html.erb into a partial _form.html.erb. form_for will figure out if the object is a new record and POST to either the create or update action, as appropriate.

I have seen some situations in the past where route changes reloaded in development mode confuse the routing code, which is usually fixed by restarting the server.

rake routes is also a good debugging tool. Check the page source to see what Rails has used for the form's action attribute, then scan down the output of rake routes to see where the request will end up.

Steve Madsen
The only thing in my routes specified for probes is map.resources :probesRoR just did the people/124321/edit by itself when I generated the controller (I'm assuming)
Ryan
Posted my rake routes
Ryan
Is that the entire 'rake routes' output? Are there any routes that aren't using resources, but instead the older :controller/:action/:id form? Also, the ID for your Probe object looks different, from your comment in John's answer. There are embedded "+" characters there. Rails will probably interpret those as URL-encoded spaces, which may be confusing the router. Finally, what error do you get when you switch to the 'form_for' syntax John and I suggested?
Steve Madsen
I found the issue, forgot to update this. It had to actually do with a naming convention that was getting mixed up in rails. It was messing up more of what I thought (instead of just this issue) and after tracking it down for awhile, I was able to use the normal form for that John suggested. Both of you got me on the right track though, thank you. Appreciate it very much.
Ryan
+2  A: 

If you're using RESTful resources, then you should just be able to do this:

<% form_for(@probe) do |f| %>
  .
  .
  .
  <%= f.submit 'Submit' %>
<% end %>

Rails can work out whether you're creating a new record or updating an existing one. See Binding a Form to an Object for further details.

Also, note the use of f.submit in my example. The *_tag helpers go together, so you wouldn't usually see a form_for helper with a submit_tag.

John Topley
When I do this I get an error around the form_for line. The error occurred while evaluating nil.to_sym
Ryan
That's probably because your @probe instance variable is nil in your controller's edit action (method). The edit action displays a form suitable for editing a model. The form then submits to the update action.
John Topley
What is your value for @probe?
Kathy Van Stone
From ruby debugger in the edit action..........irb(#<ProbesController:0x62d8f74>):001:0> @probe=> #<Probe id: "PoXmvhvk+ORIH+p7", name: "gragrag", user_id: 1, online: nil, created_at: "2009-11-10 19:16:12", updated_at: "2009-11-10 19:16:12", status: 3, street: "ragragra", city: "ragragra", country: nil, zip: "", state: "KS", contact_name: "", contact_number: "", deleted: 0>
Ryan
How about your show us the ProbesController code?
John Topley
Posted controller code
Ryan
Thanks for that Ryan.
John Topley
A: 

"Unknown Action. No action responded to (id)." sounds like a routing problem.

Have you removed or commented out the default routes in your routes.rb file?

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

If the default routes are being run it could be swapping the id for the action?

JosephL