Reputation: 527
I'm new to Ruby on Rails and I have been trying to create a sample apple but I've been stuck on this one part for WEEKS ! I have been spamming stackoverflow but I've had no luck :(. I'm trying to create a products page that also allows for multiple image upload. So I have a user model a product model and a photo model. When i submit the form filled with photos and other inputs I get this error.
NoMethodError in ProductsController#create
undefined method `photo' for #<Product:0x9078f74>
new product page
= form_for @product, :html => {:multipart => true} do |f|
%p
= f.label :description
= f.text_field :description
= fields_for @photo, :html => {:multipart => true} do |fp|
= fp.file_field :image
%p.button
= f.submit
products controller
def new
@product = Product.new
@photo = Photo.new
end
def create
@photo = current_user.photos.build(params[:photo])
@product = current_user.products.build(params[:product])
end
product model
attr_accessible :description, :name, :photo, :image
belongs_to :user
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
validates :user_id, presence: true
validates :description, presence: true
validates :photo, presence: true
end
photo model
attr_accessible :image
belongs_to :product
validates_attachment :image, presence: true
user model
attr_accessible :email, :name, :password, :password_confirmation, :image, :photo
has_many :products, dependent: :destroy
has_many :photos, :through => :products
end
tables
User
product
photo
Upvotes: 1
Views: 524
Reputation: 2908
In your products model
has_many :photos
accepts_nested_attributes_for :photos
attr_accessible :description, :name, :photos_attributes
Upvotes: 1
Reputation: 2672
Change the Products controller to
def new
@product = Product.new
@product.photos.build
end
def create
@product = Product.new(params[:product])
end
Since you have asked for multiple image upload try adding this to your view
<%= f.fields_for :photos do |img| %>
<%= render "img_fields", :f => img %>
<% end %>
<div class="add_image"><%= link_to_add_fields "Add Image", f, :photos %></div>
and create a file _img_fields.html.erb to the views and add
<div class="entry_field">
<label>Image :</label>
<%= f.file_field :image %>
<%= link_to_remove_fields "remove", f %></div>
Then add the following lines to your application.js file
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".entry_field").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
In your products model
has_many :photos
accepts_nested_attributes_for :photos
attr_accessible :description, :name, :photos_attributes
Upvotes: 4
Reputation: 35541
Change:
= fields_for @photo, :html => {:multipart => true} do |fp|
To:
= fields_for :photos, :html => {:multipart => true} do |fp|
And in your controller:
def new
@product = Product.new
@product.photos.build
end
And in your Product model:
attr_accessible :description, :name, :photos_attributes
Upvotes: 1