2

I have Person model:

class Person
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes
  include Mongoid::Paperclip

  attr_accessible :photo,:name
  has_mongoid_attached_file :photo, :styles => {:thumb => "100x100>" }, default_url: "/images/:style/missing.png"

  field :photo
  field :name  
end  

And in my update action of ManagedbController I do:

def edit
    @person=Person.find_by(name: params[:name])
end

def update
    @person=Person.find_by(name: params[:name])
    @person.update_attributes(photo: params[:photo])
end  

edit.html.erb

<h2> Add photo here!</h2>
<%= @person.name %>

<%= form_for @person, url: {controller: :managedb,action: :update}, html: {mulitpart: true}  do |f| %>
  <p><%= f.hidden_field :name %></p>
  <p><%= f.file_field :photo %></p>
  <p><%= f.submit :submit , class: "btn btn-large btn-success" %></p>
<% end %>  

when I visit the edit page I'm getting the name of the person from @person.name which means that the @person variable is NOT NIL

But when I select a image and click on submit I get this error on the update method:

Mongoid::Errors::DocumentNotFound

Document not found for class Person with attributes {:name=>nil}.

At

 @person=Person.find_by(name: params[:name])  

Request parameters

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"jpm+Ljk6rvZP9bIUw2gA9BvfZXsnATzsIpEEJMTbuzY=", "person"=>{"name"=>"child", "photo"=>#<ActionDispatch::Http::UploadedFile:0x007ffa20a05330 @original_filename="deepika-padukone-61a.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"person[photo]\"; filename=\"deepika-padukone-61a.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/b2/v1ytdy497fj9md28f7pr9wgm0000gn/T/RackMultipart20130911-3884-daf5yu>>}, "commit"=>"submit", "controller"=>"managedb", "action"=>"update"}

Rack session

What is wrong here? Help! I'm using Mongoid and Rails 3.2.13.

Community
  • 1
  • 1

1 Answers1

1

You do not give the name of the person in the form, so params[:name] is nil. You can add a hidden field to keep the name (id is better) an get it in your controller with params.

Try this :

<h2> Add photo here!</h2>
<%= @person.name %>

<%= form_for @person, url: {controller: :managedb,action: :update}, html: {mulitpart: true}  do |f| %>
  <p><%= f.hidden_field :name %></p>
  <p><%= f.file_field :photo %></p>
  <p><%= f.submit :submit , class: "btn btn-large btn-success" %></p>
<% end %> 

EDIT

I suggest you to separate your action in ManageDbController : 1 to display the form (should be edit) and another 1 to update your model (update action don't change).

You can also install this 2 gems better_errors and binding_of_caller, they can help you to debug your issue.

JoJoS
  • 1,971
  • 2
  • 13
  • 15
  • now it's giving **uninitialized constant PersonsController**. Check my question I have updated it. The update action is in another controller and there's no controller as **PersonsController** –  Sep 11 '13 at 14:23
  • look at the *action* attribute in the generated html and check the url. It seems `from_for` doesn't care *url* param. – JoJoS Sep 11 '13 at 14:48
  • I'm not understanding what's happening. Now I'm again getting the **Mongoid::Errors::DocumentNotFound**!! The multipart is working though. Updated the question. –  Sep 11 '13 at 15:15
  • did the `@person` is updated anyway ? the image is uploaded in your file system ?? – JoJoS Sep 11 '13 at 19:15
  • i've updated my answer ;). If it doesn't resolve your problem, show your routes please. – JoJoS Sep 12 '13 at 14:51
  • I'm already using the those two gem's. Will try those things now. –  Sep 12 '13 at 17:11
  • updated the question with what u had suggested. added the edit method and the form fields to `edit.html.erb`. Still getting the **DocumentNotFound error.** –  Sep 14 '13 at 06:58
  • The error is different. In Mongoid the id is alphanumeric. And rails, in the update method, works only with integer id's. See [this](http://stackoverflow.com/a/18822028/2675010) –  Sep 18 '13 at 08:26