Reputation: 5629
I want to add a picture to a user by using paperclip in my rails application
my form looks like this:
<%= form_tag :controler => "users", :action => :uploadimage, :class => "form-horizontal", html: { multipart: true }, :method => :post do |f| %>
<input type="hidden" id="user" name="userid" value="<%= @user.id %>" />
<div class="form-group">
<div class="col-sm-3"></div>
<div class="col-sm-9">
<input type="file" name="upload[image]" required="true" class="form-control"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-3"></div>
<div class="col-sm-9">
<input type="submit" class="btn btn-default">
<%#= f.submit t("main.save"), :icon => :save, :onclick => "validate(); return false;" %>
</div>
</div>
<% end %>
the upload action looks like this:
def uploadimage
if request.post?
@user = User.find(params[:userid])
if @user.update_attribute(:image, params[:image])
flash[:notice] = t("flash.saved")
redirect_to :action => :settings
end
end
end
Its done properly, but in database it's nothing added and in filestorage also not.
user model has this:
has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" },
:path => ":rails_root/public/uploads/:id/:filename",
:url => "/uploads/:id/:filename"
validates_attachment :image, :content_type => { :content_type => "image/jpg" }
Database migration is done properly all needed fields were setup in users table.
What could be the problem?
Upvotes: 0
Views: 23
Reputation: 33552
The problem is that the params[:image]
should be params[:upload][:image]
because the name
of the input field in the form is upload[image]
def uploadimage
if request.post?
@user = User.find(params[:userid])
if @user.update_attribute(:image, params[:upload][:image])
flash[:notice] = t("flash.saved")
redirect_to :action => :settings
end
end
end
Upvotes: 1