CarrierWave save NULL at file column - ruby-on-rails

im using model called Photo that references Uploader class..
class Photo < ActiveRecord::Base
attr_accessible :title, :album_id
belongs_to :album
mount_uploader :photo_image, PhotosUploader
end
class Album < ActiveRecord::Base
attr_accessible :title, :autor, :photos_attributes
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos
end
but.. when i try to save new Album (or edit, whatever..) with image it not save a file (the collumn photo_image is saved as NULL and file not stored too.
... views/albums/_form.html.erb
<%= f.fields_for :photos do |f| %>
<div class="field">
<%= f.label :photo_image %><br />
<%= f.file_field :photo_image %>
</div>
<% end %>
any suggestion?

just try to add :photo_image to attr_accessible at Photo model, it's maybe could help you.
Also check your log/development.log

Related

mxn relation in Data Models in Ruby on Rails

i have the following Problem:
I have three different data models : customer , periods and request. A request consist of the customer_id and the periods_id (i dont know why its periodS_id because i genereated a scaffold called period). Now if i want to create a request i want to choose a customer and a period so my code in the form.html.erb of requests is:
<div class="field">
<%= f.label 'Kunde auswählen' %><br />
<%= f.collection_select :customer_id, Customer.all, :id, :name %>
</div>
<div class="field">
<%= f.label 'Zeitfenster auswählen' %><br />
<%= f.collection_select :periods_id, Period.all , :id, :description %>
</div>
and this works pretty well. I can choose the name of the customers and peridos but then if i want to create a request i get the error message undefinded method, because he cant display the name of periods in the show.html.erb:
<%= #request.customer.name%>
<%= #request.periods.name%>
My idea is a mistake in the datamodels. I´m not sure if this mxn relationship works. Here the datamodels:
class Request < ActiveRecord::Base
validates :periods_id, :customer_id, presence: true
belongs_to :customer
belongs_to :period
end
class Period < ActiveRecord::Base
has_many :requests, :dependent => :destroy
has_many :customers, :through => :requests
end
class Customer < ActiveRecord::Base
has_many :requests, :dependent => :destroy
has_many :periods, :through => :requests
end
and it works for customer: if i delete period.name he can show the name of the specific customer.
Any ideas ?
Its give error of period because request model belongs to period, not request has_many period
<%= #request.period.name%>
Change .periods to .period

How can I mass assign parameters in a polymorphic association with Paperclip?

I have this code in one of my Models
class Admin::About < ActiveRecord::Base
attr_accessible :address1, ..., :assets_attributes
has_many :assets, as: :imageable
accepts_nested_attributes_for :assets
This in the Polymorphic table
class Admin::Asset < ActiveRecord::Base
attr_accessible :imageable_id, :imageable_type, :position, :image
belongs_to :imageable, polymorphic: true
has_attached_file :image
And in the form I added this
<%= f.fields_for :admin_assets do |asset_fields| %>
<%= asset_fields.file_field :image %>
<% end %>
When I submit the data with the image I got this error
Can't mass-assign protected attributes: admin_assets
I can't find the problem. Perhaps something is missing?
There is no admin_assets attribute. I think you mean assets_attributes. It's what you have defined here accepts_nested_attributes_for :assets and then above in attr_accessible :address1, ..., :assets_attributes.
Change your fields_for to:
<%= f.fields_for :assets_attributes do |asset_fields| %>

rails list all items for a many to many including design

So I have the following models:
Image:
class Image < ActiveRecord::Base
has_many :product_images
has_many :products, :through => :product_images
attr_accessible :asset, :name, :description, :product_ids, :file_content_type, :is_boolean
accepts_nested_attributes_for :product_images
has_attached_file :asset
end
ProductImage:
class ProductImage < ActiveRecord::Base
belongs_to :product
belongs_to :image
attr_accessible :is_thumbnail
end
and Product:
class Product < ActiveRecord::Base
has_many :images, :through => :product_images
has_many :product_images
attr_accessible :name, :description, :thumbnail, :searchTerms, :group_ids, :upload_file_ids
end
Now what I would like to do on the images form is display a checkbox for all the products and then another checkbox for the is_thumbnail attribute
I have had a look into using simple_fields_for but this will only display if the product has already been added. Is there a way to do this?
<%= f.simple_fields_for(:product_images) do |builder| %>
<%= builder.input :is_thumbnail %>
<%= builder.association :products, include_blank: false %>
<% end %>
I'm not very familiar with simple_fields however building form inputs base on an instance will only allow you to "represent" that instance.
This means that you could print all already associated products using
builder.association :products
but if you want to print all products in your database you will need to fetch them, loop and display them in your form.

How do I include has_many relationships in a Rails form?

I have two models, Artist and User that are connected through a third model, ArtistMembership.
From the edit/new Artist form, I want to be able to edit the role of any User in an existing ArtistMembership relationship for that Artist, delete ArtistMemberships, and add new AtistMembership relationships, which would include a User and :role.
Here's my Artist model:
class Artist < ActiveRecord::Base
has_many :artist_memberships, foreign_key: "artist_id", dependent: :destroy
attr_accessible :bio, :created_at, :email, :location, :name, :updated_at, :website, :pic
accepts_nested_attributes_for :artist_memberships, :allow_destroy => :true
...
end
Here's my User model:
class User < ActiveRecord::Base
...
has_many :artist_memberships, foreign_key: "user_id"
...
end
Here's my ArtistMembership model:
class ArtistMembership < ActiveRecord::Base
belongs_to :artist, class_name: "Artist"
belongs_to :user, class_name: "User"
attr_accessible :artist_id, :created_at, :role, :user_id
end
If I have a _form.hml.erb too, for editing Artists that starts:
<%= form_for #artist do |artist_form| %>
<div class="field">
<%= artist_form.label :name %>
<%= artist_form.text_field :name %>
</div>
..
<div class="actions">
<%= artist_form.submit %>
</div>
<% end %>
how can I create the related ArtistMembership forms for the aforementioned functionality?
May be this is helpful for you, see this field_for
you can use accepts_nested_attributes_for(*attr_names)
Maybe you are looking for this method.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
Refer to the "One-to-many" section.
But if I were you, I would rather use the "Nested Resource" technic.
http://guides.rubyonrails.org/routing.html#nested-resources

Carrierwave saves object data to database instead of file name

Hi
I just can not find out what is wrong with my code. I have two models Items and Images and relation between them
class Item < ActiveRecord::Base
attr_accessible :category_id, :user_id, :title, :description, :published, :start_date, :end_date, :images_attributes
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
mount_uploader :name, ImageUploader
end
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
attr_accessible :item_id, :name
end
I call the 3 instances of Carrierwave in items_controller.rb to add 3 images to the created item
def new
#item = Item.new
3.times { #item.images.build }
end
The view form looks like this :
<%= f.fields_for :images do |builder| %>
<p> <%= builder.text_field :name %> </p>
<% end %>
Which resoults in this randered code :
<input id="item_images_attributes_0_name" name="item[images_attributes][0][name]" type="file">
When Add and save the new item i get object data saved instead of file name ( suit.jpg ) in my database :
--- !ruby/object:ActionDispatch::Http::UploadedFile
content_type: image/jpeg
headers: |
Content-Disposition: form-data; name="item[images_attributes][0][name]"; filename="suit.jpg"
Content-Type: image/jpeg
original_filename: suit.jpg
tempfile: !ru
Screen shot from database table below :
https://lh3.googleusercontent.com/_USg4QWvHRS0/TXDT0Fn-NuI/AAAAAAAAHL8/91Qgyp5jK3Q/carrierwave-objest-database-saved.jpg
Is anyone who have an idea how to solve it ?
I had a similar issue, I wanted to create my own filenames (identifiers) so I defined a filename method on the uploader (in your case ImageUploader)
e.g.
def filename
#name ||= "foo"
end
First things first, Your mounting your uploader to a column named :image but from your pic of your db you dont have a column with said name.
1: Make a column for images called image ("since thats what your uploading.")
rails g migration add_image_to_images image:string
rake db:migrate
2: Update your model's attr_accessible to use the new column.
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
attr_accessible :item_id, :name, :image
end
3: Update your view
<%= f.fields_for :images do |builder| %>
<p>
<%= builder.text_field :name %>
<%= builder.file_field :image %>
</p>
<% end %>
4: remove the unused mount from the Item class.
class Item < ActiveRecord::Base
attr_accessible :category_id, :user_id, :title, :description, :published, :start_date, :end_date, :images_attributes
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
end
I left :name in place on Image for use as an arbitrary value you can add to your image.
Also by abstracting your image model like you did I would also assume you want to keep track of the image order so maybe an extra column for that would be a good idea too.
Hope this helps.

Resources