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
Related
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
I'm building a simple app with standard User model that has_one Profile model. I would like the profiles table to have a column for an image attribute. Therefore, I followed RailsCast #253 titled CarrierWave File Uploads. All is going well until I try to resize an image that has been uploaded. This requires the installation of ImageMagick & RMagick which took an entire day of searching to get done. However, I think I finally got it right and successfully installed rmagick version 2.13.2 (as verified by running "gem list").
But not so fast...now when I try to render the form to create a new profile, I get the following error:
NoMethodError in Profiles#new
undefined method `model_name' for NilClass:Class
FYI, this form was working fine until I un-commented "include CarrierWave::RMagick" in my ImageUploader (which I'm suppose to do if I want to use RMagick methods for image re-sizing).
Any thoughts on how to fix this?
My version info (I used RailsInstaller for Windows to get up & running)
Rails 3.2.13
Ruby 1.9.3p392 [i386-minw32]
ImageMagick 6.8.5-Q16
rmagick 2.13.2
Gemfile
gem 'rmagick'
gem 'carrierwave'
Models
class User < ActiveRecord::Base
has_one :profile
class Profile < ActiveRecord::Base
attr_accessbile :image
belongs_to :user
mount_uploader :image, ImageUploader
ImageUploader
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
ProfilesController
def new
#profile = current_user.build_profile
end
views/profiles/new.html.erb
<%= form_for #profile, :html => {:multipart => true} do |f| %>
<%= f.file_field :image %>
<%= f.submit "Create my profile" %>
<% end %>
Just replace this line
<%= form_for #profile, :html => {:multipart => true} do |f| %>
by
<%= form_for #instructor_profile, :html => {:multipart => true} do |f| %>
Issue is really with form_for not with RMagick, you have defined #instructor_profile in controller but using #profile in view. Make sure that there will be single space between form_for and #instructor_profile
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
I have a very simple rails 3 program with 2 models: a user model for Devise and a writing model that captures a text field and the user's id.
My routes file is pretty basic:
devise_for :users
resources :users, :writings
root :to => "users#index"
And my form for writings, using Formtastic, is as well:
<% semantic_form_for(#writing, :html => {:method => :put}) do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
When I try to create a new writing, the form looks great, but then when I hit submit, I get the following error:
No route matches "/writings"
I've run rake routes, and everything else seems to be working on, and I am using the default generate scaffold from rails, so the controller is the out of the box controller.
Any ideas on where I went astray?
Chris, try putting the declaration of the form like this
<% semantic_form_for #writing do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
I've the idea that when you specify the :html parameter, you "override" some defaults in formtastic. Sorry, I'm not an expert on formtastic. I've used a bit and then decided to go for simple_form :).
I have pulled out all my hair. No more left... :(
I am using Spree 0.3.4, within an extension I need to register some retailers up. so I direct them to a retailers form which has many custom fields which belong to a retailer model...
So I am trying to validate/submit all the fields from one form like so
myextension/app/views/user_registrations/new.html.erb
<%= form_for (:user, :url => registration_path(#user, :type => "retailer) do |f| %>
<%= f.fields_for :retailer do |r| %>
<%= r.text_field :name %>
<% end %>
<%= f.text_field :email %>
<% end %>
etc etc
class Retailer < ActiveRecord::Base
belongs_to :user
validates :name,
:presence => true
end
class User < ActiveRecord::Base
has_one :retailer
accepts_nested_attributes_for :retailer
attr_accessible :retailer_attributes
# theres a whole lot more spree and devise stuff here. not sure worth mentioning
end
I have also added the abilities in the cancan ability.rb
The problem is the retailer feilds never get validated and the data is never inserted into the database...
I created a blank app, and tried this process from scratch with some plain old scaffolding and it works fine.
any ideas??
In your application helper, do something like this(assuming your have Ruby 1.9.* for the tap functionality, otherwise checkout rails returning here):
def setup_user(user)
user.tap do |u|
u.build_retailer if u.retailer.nil?
end
end
then in your view change it to this:
<%= form_for (setup_user(#user), :url => registration_path(#user, :type => "retailer) do |f| %>
See if that works.