Domanda

So far,i got a Model "utilisateur" with basic informations. I want to add a banner picture. I ran rails g uploader banner , rails g migration AddBannerToUtilisateur banner:string, and add mount_uploader :banner BannerUploader to the model ( of course i ran db:migrate and restart the server ).

If i create a new "utilisateur", i think upload should work (it works for the "utilisateur"'s posts ), but here, i just want to update the current "utilisateur" and attach a new banner.

I can't use UPDATE from the controller because i will have to provide all the "utilisateur" infos. I want to make a separate updatebanner function.

Here is my form :

<%=form_for :utilisateur, :url => updatecover_path, :html => {:multipart => true} do |f|%>  
<%=f.file_field :banner%>  
<%=f.submit%>
<%end%>

Here is my controller :

def updatecover
@utilisateur = Utilisateur.find(current_user.id)
@utilisateur.banner = params[:utilisateur[:banner]]
@utilisateur.save!

end

I know and i understand why this function is not working, but i don't know what to do. Can't find a precise answer or tips on the web.

Here is the request :

Started POST "/updatecover" for 127.0.0.1 at 2014-04-04 12:26:53 +0200 Processing by UtilisateursController#updatecover as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"rncjd3meVuQuSLHxPrwbtEF7Ye0hH7pcNxcLoYy2oeQ=", "utilisateur"=>{"banner"=>#, @original_fil ename="2048.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"utilisateur[banner]\"; filename= \"2048.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Save Utilisateur"}

here's my route:

match "updatecover" => "utilisateurs#updatecover", :as => :updatecover,:via => [:get,:post]

I think my error is basic. But i can't find out.

Thanks

Here's the full log

Started POST "/updatecover" for 127.0.0.1 at 2014-04-04 15:42:19 +0200 Processing by UtilisateursController#updatecover as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"rncjd3meVuQuSLHxPrwbtEF7Ye0hH7pcNxcLoYy2oeQ=", "utilisateur"=>{"banner"=>#, @original_fil ename="shift-s3ctor-airstrip-attack-october-2013-preview-36304 (1).jpg", @content_type="image/jpeg", @headers="Content-Disposit ion: form-data; name=\"utilisateur[banner]\"; filename=\"shift-s3ctor-airstrip-attack-october-2013-preview-36304 (1).jpg\"\r\nC ontent-Type: image/jpeg\r\n">}, "commit"=>"Save Utilisateur"} Utilisateur Load (0.3ms) SELECT "utilisateurs".* FROM "utilisateurs" WHERE "utilisateurs"."id" = $1 LIMIT 1 [["id", 7]] CACHE (0.0ms) SELECT "utilisateurs".* FROM "utilisateurs" WHERE "utilisateurs"."id" = $1 LIMIT 1 [["id", 7]] CACHE (0.0ms) SELECT "utilisateurs".* FROM "utilisateurs" WHERE "utilisateurs"."id" = $1 LIMIT 1 [["id", 7]] Completed 500 Internal Server Error in 6ms

TypeError (no implicit conversion of Symbol into Integer): app/controllers/utilisateurs_controller.rb:28:in []'

app/controllers/utilisateurs_controller.rb:28:inupdatecover'

È stato utile?

Soluzione

Update the updatecover action as below:

def updatecover
  @utilisateur = Utilisateur.find(current_user.id)
  @utilisateur.banner = params[:utilisateur][:banner] ## Notice how params is called
  @utilisateur.save!
end

If you notice the params hash:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"rncjd3meVuQuSLHxPrwbtEF7Ye0hH7pcNxcLoYy2oeQ=", "utilisateur"=>{"banner"=>#, @original_fil ename="2048.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"utilisateur[banner]\"; filename= \"2048.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Save Utilisateur"}

params[:utilisateur] would give you:

{"banner"=>#, @original_fil ename="2048.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"utilisateur[banner]\"; filename= \"2048.jpg\"\r\nContent-Type: image/jpeg\r\n">}

Now, to get banner out of that, what you need is params[:utilisateur][:banner] which would yield:

#, @original_fil ename="2048.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"utilisateur[banner]\"; filename= \"2048.jpg\"\r\nContent-Type: image/jpeg\r\n">
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top