3

I have two methods in PersonsController- edit and update and a Person model:

def edit
  @person=Person.find(params[:id])
end

def update
    @person=Person.find(params[:id])
    @person.update_attributes(params[:person])
end  

and my edit.html.erb:

<h2> Edit info here!</h2>
<%= @person.name %>

<%= form_for @person, html: {mulitpart: true}  do |f| %>
  <p><%= f.text_field :location %></p>
  <p><%= f.submit :submit , class: "btn btn-large btn-success" %></p>
<% end %>  

and routes.rb:

resources :persons

But when I submit the form I get:

AbstractController::ActionNotFound
The action '5231d2491dba7fb057000004' could not be found for PersonsController

The 5231....... is id of a person.

Request Parameters:

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"3TL7nn4UxhxFoxETeooBMThhkE0VdMoGFpjN9sx4srk=", "person"=>{"location"=>"nagpur"}, "commit"=>"submit", "controller"=>"persons", "action"=>"5231d2491dba7fb057000004"}

What is wrong here? I'm using mongoid and rails 3.2.13.

mrudult
  • 2,480
  • 3
  • 35
  • 54
  • please show routes for persons. – Billy Chan Sep 14 '13 at 07:56
  • @BillyChan I have `resources :persons` in routes. But I don't think that's required for a **Person** model. Routes are for controllers only(?) – mrudult Sep 14 '13 at 07:59
  • 1
    I doubt how comes `params[:name]` because if everything as convention there is no such param except `params[:id]`. – Billy Chan Sep 14 '13 at 08:01
  • @BillyChan I have a fields `name` and `location` for my Person's model – mrudult Sep 14 '13 at 08:02
  • I know, but that's only model method. How come it becomes a param? – Billy Chan Sep 14 '13 at 08:03
  • @BillyChan I'm not getting you.Sorry, I'm new to rails.That `params[:name]` work well in the edit action. I'm passing the `name` from the form's `hidden_field`. What would you suggest otherwise? – mrudult Sep 14 '13 at 08:12
  • Looks fine, not sure the reason. Maybe the cache issue, try manually remove cache in browser, and cache in /tmp, restart server. – Billy Chan Sep 14 '13 at 09:15
  • i don't think that's cache issue. Tried everything possible. But the error thrown in also wierd. Thanks for help. @BillyChan – mrudult Sep 14 '13 at 09:33

1 Answers1

1

Your final comment reveals the source of error.

For either #edit or #update, you should not set the params manually in hidden field, which is also unnecessary.

If using conventional RESTful routes as resources :persons, you edit route will look like GET /persons/1/edit where 1 is the param id. You can get this person in #edit as

@person = Person.find(params[:id])

Similarly, you can get the person again in #update whose route is PUT persons/1

@person = Person.find(params[:id])

Then the question is answered.

But there is one more problem in your code, the part you get the attributes for updating. Because the params of person's attributes are sent via form, all attributes are under params[:person]. Thus, to get the photo attribute, you should use

params[:person][:photo] # Not params[:photo]
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • Still it cannot find person with the id in the update method. Gives **Calling Document.find with nil is invalid.** I would like to add is that I don't have any **PersonsController** – mrudult Sep 14 '13 at 08:38
  • Remove the `url: {...}` part in form, it's unnecessary. – Billy Chan Sep 14 '13 at 08:46
  • That may be the reason no param conveyed, so remove it. – Billy Chan Sep 14 '13 at 08:48
  • Can't. As I said I don't have a **PersonsController**. otherwise @person searches for a PersonsController and Gives **uninitialized constant PersonsController** (tried this) – mrudult Sep 14 '13 at 08:48
  • I think why that is working with #edit is because the link which directs to edit page, I'm passing person's id as a parameter. But we aren't passing any parameter for the #update. May be I'm wrong! – mrudult Sep 14 '13 at 08:55
  • hmmm, `resources :persons` but no persons controller, not nice at all. Anyway, fire your `rake routes` to check the path name for updating a person, use that route with person as argument, like `url: managedb_path(@person)`. – Billy Chan Sep 14 '13 at 08:56
  • Updated the question. I have added the **PersonsController** now. – mrudult Sep 14 '13 at 09:10
  • That has nothing to do with your question, neither route. It's the form's error. Try remove `:submit` in submit line, to read `f.submit "Submit", class: {blah blah}` – Billy Chan Sep 18 '13 at 08:58