Rails paperclip params not being sent when updating user - ruby-on-rails-3

I am installed paperclip, but no params are being sent. Here is what I have...
#app/views/users/edit.html.erb
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
#app/models/user.rb
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
app/controllers/users_controller.rb
def update
#user = current_user
respond_to do |format|
if #user.update_attributes(params[:user])
...
Any ideas? Not sure where to go next. I don't even see 'avatar' in my params hash.

At a glance the code you have there looks OK. Can you paste in the request Logs located in the console, or logs/development.log. It should help give some insight to the issue.
I wrote an article Uploading Files to S3 in Ruby with Paperclip on the Heroku dev center. The repo has example code on how to set up Paperclip. Might help to reference that?

I needed to add the code...
<%= f.file_field :avatar %>
to my app/views/users/_form.html.erb file rather than app/views/users/edit.html.erb

Related

Uploading Images using paperclip in rails 3

I am trying to upload an image in my Rails Application. However, I am unable to do so. I am relatively new to rails and I need some help here.
I have a "Picture" Model shown here -
class Picture < ActiveRecord::Base
attr_accessible :photo, :tag
has_attached_file :photo, :styles => { :medium => "300x300>",
:large => "1000x1000>", :thumb => "100x100>" }
end
I have a "Pictures" Controller shown here -
class PicturesController < ApplicationController
def new
#picture = Picture.new
end
def create
#picture = Picture.create( params[:picture] )
if #picture.save
flash.now[:success] = "Image Uploaded"
redirect_to #picture
else
render 'new'
end
end
def index
end
def show
#picture = Picture.find(params[:id])
send_data #picture.photo, :type => 'image/png', :disposition => 'inline'
end
def imageshow
end
end
Lastly, These are my "new" and "show" views:
"new.html.erb"
<h1>Upload an Image</h1>
<div>
<%= form_for(#picture, :html => {:multipart => true}) do |f| %>
<%= f.file_field :photo %>
<%= f.label :tag %>
<%= f.text_field :tag %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
"show.html.erb"
<h1>Displaying Uploaded Image</h1>
<div>
<%= image_tag #picture.photo.url %>
<%= image_tag #picture.photo.url(:large) %>
</div>
I am able to reach the Upload page (ie, new.html.erb in Pictures Controller). However, when I upload an image I get the following error:
"This image "http://10.102.119.20:3000/pictures/12" cannot be displayed because it contains errors"
My queries are:
Where does an image get uploaded on the server?
Is there any configuration that needs to be done?
Is there any other issue with my code?
Thanks in advance for any help..!!
Ryan Bates discusses Paperclip in his Railscasts. Check it out. It helped me get started.

Rails best_in_place gem with nested resource

Does anyone know if it is possible (and, if so, what the syntax is) for using a nested resource with the best_in_place gem?
My routes.rb looks something like this
resources :users do
resources :goals
end
I would like to edit the :description field of the goal, but the code in my view for
<%= best_in_place [#user, #goal], :description %>
gives a NoMethodError saying
undefined method `description' for #<Array:0x20e0d28>
Using
<%= best_in_place #goal, :description %>
give me an undefined method error also because there is no goal_path
I can get the gem to work for #user (the non nested resource) field without problems.
I'm running Rails 3.1.1, Ruby 1.9.2, best_in_place 1.0.4
I figured it out.
I needed to set the path option in the call like so
<%= best_in_place #goal, :description, :path => user_goal_path %>
It works like a champ now!
Add path and the objects to the path:
<%= best_in_place #goal, :description, :path => user_goal_path(#user,#goal) %>
Somehow the simple path solution of bknoles didn't work for me.
Now above method is deprecated.
According to latest Documentation use ":url" instead of ":path" like below in the example
<%= best_in_place #goal, :description, :url => user_goal_path %>
Cheers!
Thank you, #bknoles. Your answer definitely helped me reach a similar solution of my own. Here's my implementation:
#widget.rb
class Widget < ActiveRecord::Base
validates_presence_of :name
has_many :gadgets
attr_accessible :name, :description
end
#gadget.rb
class Gadget < ActiveRecord::Base
belongs_to :widget
attr_accessible :name, :widget_id, :id
end
#gadgets_controller.rb
def update
#gadget=#widget.gadgets.find(params[:id])
if #gadget.update_attributes(params[:gadget])
respond_to do |format|
format.html
format.json { respond_with_bip(#gadget) }
end
else
respond_to do |format|
format.html { render :action => "edit" }
format.json { respond_with_bip(#gadget) }
end
end
end
#views/gadgets/_gadget.html.haml
%tr{ :name => "gadget_", :id => gadget.id }
%td= gadget.created_at.localtime.strftime("%B %d, %l:%M%p")
%td.big=best_in_place gadget, :name, :path => [#widget, gadget]
%td.delete{:style => 'text-align:center;'}
=check_box_tag "gadget_ids[]", gadget.id, false, :class => "checkbox"
You can checkout the entire project on github if you want to see more of the code.
https://github.com/hernamesbarbara/ajax-rails-full-crud
Best,
Austin

rails 3, paperclip assigns id 0 when uploading image

iam using rails with formtastic for my admin backend. i want to be able to upload an image to my recordset, and i try to use paperclip to to that.
when i edit a recordset, the upload of the image works just fine. when i try to CREATE a NEW recordset, paperclip seems to assign the ID 0 for that image in my upload path!
#expected path for new image:
/public/logos/2342/some_image.png
#and thats what i get when i create my new record-set:
/public/logos/0/some_image.png
i tried to add attr_accessible to my model
attr_accessible :logo_file_name, :logo_content_type, :logo_file_size, :logo_updated_at
but that throws me an sql-error
Column 'logo_file_size' cannot be null
EDIT: solved the mysql error when i add attr_accessible. i just allowed the logo_file_size to be null. but the id=0 problem still exists...
my code:
MODEL:
has_attached_file :logo,
:url => "/:class/:attachment/:id/:basename.:extension",
:styles => { :original => ["150x150>", :png] }
VIEW:
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :logo, :as => :file %>
<%= f.input :link, :as => :url %>
<%= f.input :published, :published => 'Veröffentlicht' %>
<% end %>
CONTROLLER:
def create
Article.create(params[:article])
end
my datebase has these 4 colums in Article-Table:
logo_file_name
logo_content_type
logo_file_size
logo_updated_at
iam using rails 3.1.1, formtastic 1.2.4, paperclip 2.4.5
thanks a lot for your help!!!
i know its a bit late, but i found the problem and will share the answer for everyone with the same problem.
problem was mysql, upgraded to mysql2 gem, and everything worked as expected

Rails 3 Route error - "No Route Matches"

I've been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I'm getting, which is No route matches {:controller=>"profiles", :action=>"show"}.
I get the error working my way through a Rails 3 sign-up process using nested model forms. The sign-up process, as follows:
user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save
The sign-up process starts at the homepage with the following form:
<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
<div>
...
</div>
<%= f.fields_for :profile do |f| %>
<% end %>
<% end %>
Then the process goes to fill in the profile, then redirect to the new User's profile after this form is completed:
<%= form_for :profile, :html => { :multipart => true } do |f| %>
<div>
...
</div>
<%= f.fields_for :user do |f| %>
<% end %>
<% end %>
I have accepts_nested_attributes_for :user and :profile in their respective models.
My rails server it gives me a bit more detail:
ActionController::RoutingError (No route matches {:controller=>"profile.save", :action=>"show"}):
app/controllers/profiles_controller.rb:15:in `create'
So in my ProfilesController in 'create':
def create
#profile = Profile.new(params[:profile])
if #profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
Seems clear that the issue is in profile_path, so my Routes.rb:
post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"
Can anyone help shed light on what I'm doing wrong/missing in my Routes.rb file?
The redirect path should contain the specific profile to redirect to:
if #profile.save
redirect_to profile_path(#profile), :notice => 'User successfully added.'
else
.....
Also the routes should include this line:
get "/profiles/:id" => "profiles#show", as => "profile"

Remote form submission not working with namespace on Rails3

I am working on a project which allow people to upload a personal profile, including projects and updates related to their project.
Everything is working fine except that I would like to submit my form using :remote => true but i have no way to get it to work. I could use classic jquery/ajax to submit the form, but the Rails3 UJS is great !
updates_controller.rb
def create
#update = Update.new(params[:update])
#update.user_id = current_user.id
#update.project_id = params[:project_id]
if #update.save
respond_to do |format|
format.js { render :nothing => true }
end
end
end
_form.html (views)
<%= form_for([#project, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
I tried also
<%= form_for([:profile, #project, #update], :remote => true) do |f| %>
routes.rb
namespace :profile do
resources :projects, :only => [:show, :index, :edit] do
resources :updates
end
end
If anyone has an idea how to get the :remote => true to work here , it will be great !!
Thanks to all the Stack Overflow community for all the precious resources I found here so far.
_Clement
Did you initialized #project in new action of controller?
Try to replace
<%= form_for([#project, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
with
<%= form_for([#update.project_id, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
What is the server response when you submit the form?