Uploading Multiple Images With Paperclip - Rails 4 App - ruby-on-rails

I have been through many tutorial and questions here but none seem to help me - it may be because I am new to this. The one that most closely aligns is here but still it hasn't helped me figure this out.
I am trying to upload multiple pictures with paperclip. (I have the mini_magick gem as well) I have succeeded in creating a section for a "Profile Image" using this gem but now I need a section with multiple pictures. Any help would be great. Currently no images are showing when I upload them in the product show view nor saving to a product in the database.
Product table schema
create_table "products", force: true do |t|
t.string "name"
t.text "description"
t.decimal "price"
t.datetime "created_at"
t.datetime "updated_at"
t.date "releasing_on"
t.string "website"
t.string "image_file_name"
t.string "industry"
t.string "slug"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "company"
t.string "picture_file_name"
t.string "picture_content_type"
t.integer "picture_file_size"
t.datetime "picture_updated_at"
end
(Product Contoller) products_controller.rb
class ProductsController < ApplicationController
before_action :require_signin
before_action :require_admin, except: [:index, :show]
before_action :set_product, only: [:show, :edit, :update, :destroy]
def show
#clockers = #product.clockers
#categories = #product.categories
if current_user
#current_clock = current_user.clocks.find_by(product_id: #product.id)
end
end
def edit
end
def update
if #product.update(product_params)
redirect_to #product
else
render :edit
end
end
def new
#product = Product.new
end
private
def product_params
params.require(:product).permit(:slug, :name, :description, :price, :releasing_on, :website, :company, :image, :image_content_type, :picture, :picture_content_type, category_ids: [])
end
def set_product
#product = Product.find_by!(slug: params[:id])
end
end
(Product Model) product.rb
class Product < ActiveRecord::Base
before_validation :generate_slug
...
has_attached_file :image
has_attached_file :picture
...
end
products.helper.rb (Products Helper)
module ProductsHelper
...
def image_for(product)
if product.image.exists?
image_tag(product.image.url)
else
image_tag('placeholder_sneaker.png')
end
end
def picture_for(product)
if product.picture.exists?
image_tag(product.picture.url)
else
""
end
end
end
Products -- show.html.erb (Where I'd like to show the pictures)
<h1> <%= #product.name %></h1>
<div class="container">
<div class="row">
<div class="col-md-4 thumb">
<div class="thumbnail" >
<img class="img-responsive"> <%= image_for(#product) %> <%# profile image %>
<div>
<%= picture_for(#product) %> <%# multiple pictures %>
</div>
</div>
</div>
_form.html.erb (Product Show/Edit/Update Form)
<%= form_for(#product) do |f| %>
<%= render "shared/errors", object: #product %>
<form class="form-horizontal" role="form">
<div class="form-group">
<ol>
<li class=>
<%= f.label :image, "Profile Image" %>
<%= f.file_field :image %>
</li>
<li class=>
<%= f.label :picture, "More Pictures" %>
<%= f.file_field :picture, multiple: true %>
</li>
</ol>

Related

Rails Not able to save data, association

I'm doing a parking permit website. The problem I met is that I'm not able to save my data to the PERMIT database which associated with the USER database. The problem i think is I didn't bring the user to the permit(Maybe i missed something). I found out the error when I trying to save from Permit.errors.full_messages is ["User must exist"]. Any help is appreciated, Thank you!
Schema.rb
ActiveRecord::Schema.define(version: 20160920143651) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.string "duration"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.string :duration
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_index :permits, :user_id
end
end
Permit_controller
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to #permits
else
redirect_to contact_path
end
end
def destroy
Permit.destroy_all(user_id: 1)
respond_to do |format|
format.html { redirect_to users_url, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def show
#permits = Permit.find(params[:id])
end
def update
respond_to do |format|
if #permits.update(user_params)
format.html { redirect_to #user, notice: 'Permit was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
user.rb
class User < ApplicationRecord
has_many :permits
has_secure_password
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
I guess you are using Rails 5. The problem is you have belongs_to association on permit that is belongs to user but while creating permit you are not associating any user with it and in Rails 5 it is mandatory to assign data to belongs_to association i.e you can not save permit when it don't have user_id so try to assign a user_id to permit. refer this for change to rails 5 belongs_to association
Where exactly is your error ? If in update action, you have to change your before_action. You must add there :update action.
before_action :set_permit, only: [:show, :destroy, :update]

Can't save my data into sqlite database

I am trying to implement a Parking Permit application page using ROR. I couldn't get my data saved into the database. The permit database is associated with the user also. The program won't save the data and execute the else statement. There is no error generated, i think i have missed something but i don't know the exact problem. Any help is appreciated!
Permit_controller.rb
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
def destroy
end
def show
#permits = Permit.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate,:permitstart, :permitend)
end
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_foreign_key :permits, :user
add_index :permits, [:user_id, :created_at]
end
end
User.rb
class User < ApplicationRecord
has_secure_password
has_many :permits
end
#book pg 264 Validation
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20160921071908) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
check with this #permits.save!.
it shows the exact error.
module ApplicationHelper
#for current user to use through out the app
def current_user
#current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
end
end
and
def create
#permits = Permit.new(permit_params)
#permits.user = current_user
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
test it
Or you can just say that a permit has a single user and avoid the confusion.
#models/permit.rb
class Permit < ApplicationRecord
has_one :user
end
#controllers/permit_controller.rb
def create
#user = User.find(session[:user_id]) #use your session variable
#permits = Permit.new(permit_params)
if #permits.save
#user.permits << #permits
redirect_to root_path
else
redirect_to contact_path
end
end
It will save permits for the logged in user.

rails paperclip - trouble uploading multiple images to one model through nested model

I have been trying to figure out how to upload multiple images to one model through a nested model for a while now with no luck. I have a Project model, and for each project i would like to upload multiple images. I created a model called Picture and nested it within the Project model, and have set up paperclip and everything seems fine except when I upload an image and click on "Create project", the image does not show on the "show" page. There is no error message displayed. Please help as I do not know how to proceed from here.
here is my code:
Project form:
<%= bootstrap_nested_form_for #project, :html => {:multipart => true} do |f| %>
<% f.fields_for :pictures do |builder| %>
<% if builder.object.new_record? %>
<p>
<%= builder.file_field :image %>
</p>
<% end %>
<%= builder.link_to_remove "Remove" %>
<% end %>
<p>
<%= f.link_to_add "Add Images", :pictures %>
</p>
<%= f.submit %>
<% end %>
Project controller:-
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#projects = Project.all
respond_with(#projects)
end
def show
respond_with(#project)
end
def new
#project = Project.new
#project.pictures.build
respond_with(#project)
end
def edit
#project = Project.find(params[:id])
#project.pictures.build
end
def create
#project = Project.new(project_params)
if #project.save
flash[:notice] = "Successfully created project."
redirect_to #project
else
render :action => 'new'
end
end
def update
#project.update(project_params)
respond_with(#project)
end
def destroy
#project.destroy
respond_with(#project)
end
private
def set_project
#project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:id, :title, :description, :status, :phase, :location, pictures_attributes: [:id, :image])
end
end
Projects model:-
class Project < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? }
end
Pictures model:-
class Picture < ActiveRecord::Base
belongs_to :project
has_one :image
has_attached_file :image,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
Show page:-
<% #project.pictures do |picture| %>
<%= image_tag picture.image_url %>
<% end %>
<p>
<strong>Title:</strong>
<%= #project.title %>
</p>
<p>
<strong>Description:</strong>
<%= #project.description %>
</p>
<p>
<strong>Status:</strong>
<%= #project.status %>
</p>
<p>
<strong>Phase:</strong>
<%= #project.phase %>
</p>
<p>
<strong>Location:</strong>
<%= #project.location %>
</p>
<%= link_to 'Edit', edit_project_path(#project) %> |
<%= link_to 'Back', projects_path %>
schema :-
ActiveRecord::Schema.define(version: 20150728092717) do
create_table "pictures", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "project_id"
end
add_index "pictures", ["project_id"], name: "index_pictures_on_project_id"
create_table "projects", force: true do |t|
t.string "title"
t.text "description"
t.string "status"
t.string "phase"
t.string "location"
t.datetime "created_at"
t.datetime "updated_at"
end
Your form and whitelist uses the property name image.
But you are rejecting any nested pictures if they don't have the picture param.
accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? }
Nested attributes params are not wrapped in a "model key" like rails form params usually are. This is what they look like:
params = {
project: {
pictures_attributes: [
{
image: 'foo.jpg'
}
]
}
}
You can catch these kind of errors quite simply with model specs:
require 'rails_helper'
RSpec.describe Project do
it 'accepts nested pictures' do
project = Project.new(pictures_attributes: [{ image: 'foo.jpg' }])
expect(project.pictures.first).to to_be_a Picture
end
end

Unpermitted parameters: user error when passing user params using nested attributes

I have a form that collects company information as well as the first user (the company admin). When I submit the form, the company attributes are saved to the db. However, the user attributes are not. I get the error Unpermitted parameters: user. I can't figure out why the user is not being created and saved.
I have:
class CompaniesController < ApplicationController
def new
#company = Company.new
#plans = Plan.all
end
def create
#company = Company.new(company_params)
#user = User.new
#user.role = "admin"
#user.save
if #company.save
redirect_to #company, notice: 'Company was successfully created.'
else
render action: 'new'
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def company_params
params.require(:company).permit(:name, :plan_id, users_attributes: [:id, :company_id, :email, :password, :password_confirmation, :first_name, :last_name, :role, :rate])
end
end
and
class UsersController < ApplicationController
# include UsersHelper
def index
#users = User.all
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
#user.save
flash.notice = "User '#{#user.first_name} #{#user.last_name}' was successfully created."
redirect_to user_path(#user)
end
def show
#user = User.find(params[:id])
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
#user.update(user_params)
flash.notice = "User '#{#user.first_name}' has been updated."
redirect_to user_path(#user)
end
def destroy
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name, :role, :rate)
end
end
and
class Company < ActiveRecord::Base
has_many :users
belongs_to :plan
accepts_nested_attributes_for :users, :allow_destroy => true
end
and
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates_confirmation_of :password, message: "should match confirmation", if: :password
has_many :jobs
belongs_to :company
end
and
<%= form_for(#company) do |f| %>
<% if #company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% #company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name, :id => "name" %>
</div>
<div class="field">
<%= collection_select( :company, :plan_id, #plans, :id, :name ) %>
</div>
<%= f.fields_for :user do |user| %>
<div class="field">
<%= user.label :email %><br>
<%= user.text_field :email %>
</div>
<div class="field">
<%= user.label :password %><br>
<%= user.password_field :password %>
</div>
<div class="field">
<%= user.label :password_confirmation %><br>
<%= user.password_field :password_confirmation %>
</div>
<div class="field">
<%= user.label :first_name %><br>
<%= user.text_field :first_name %>
</div>
<div class="field">
<%= user.label :last_name %><br>
<%= user.text_field :last_name %>
</div>
<div class="field">
<%= user.label :role %><br>
<%= user.text_field :role %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and
ActiveRecord::Schema.define(version: 20140421235514) do
create_table "companies", force: true do |t|
t.string "name"
t.string "stripe_token"
t.integer "plan_id"
t.integer "user_id", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "companies", ["plan_id"], name: "index_companies_on_plan_id"
add_index "companies", ["user_id"], name: "index_companies_on_user_id"
create_table "plans", force: true do |t|
t.string "stripe_id"
t.string "name"
t.integer "amount"
t.string "interval"
t.string "currency"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "email", null: false
t.string "crypted_password", null: false
t.string "salt", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "reset_password_token"
t.datetime "reset_password_token_expires_at"
t.datetime "reset_password_email_sent_at"
t.string "first_name"
t.string "last_name"
t.string "role"
t.integer "rate"
t.integer "company_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token"
end
Company and User are associated with 1-M Relationship , i.e., Company has_many :users
In that case, in your view for Company, the nested form should be
<%= f.fields_for :users do |user| %> ## Notice users in plural
and NOT
<%= f.fields_for :user do |user| %>
Refer to the Nested Attributes Examples for One to Many
Currently, fields_for is setup incorrectly with singular :user so in params hash you got the key as :user and again a warning Unpermitted parameters: user because of which the user attributes were not stored in database.
Now, as you have setup accepts_nested_attributes_for in Company model. Controller is expecting user attributes in key users_attributes within params hash.
Changing the fields_for with plural :users argument would result in creation of users_attributes key in params hash upon form submission.
UPDATE
Company has many users, its 1-M relationship
Only users table should have foreign key as company_id.
You need to remove user_id from companies table.
Also, update the CompaniesController#new action as below:
def new
#company = Company.new
#users = #company.users.build
#plans = Plan.all
end
Strong params permitting looks fine to me but i think the issue is in the nested form, you used wrong relation name user while its users which generates a params hash titled with user which is not permitted, instead you should do:
<%= f.fields_for :users do |user| %>
#rest of the form elements
<% end %>

Unpermitted parameters on multiple paperclip file uploads Rails4

I've got a ruby 2.0.0 and rails 4.0.0 app that has a 'Guitar' and 'Photos' model. I've used paperclip to upload a single file in Rails3, but am new to uploading multiple files in Rails 4. I created the second model to hold said photos, and read up on strong parameters, etc. I'm getting an error when attempting to add 3 photos to a guitar. In the logs: "Unpermitted parameters: photos_attributes". I've tried adding photos_attributes to the whitelist and no joy. I'm pulling my hair out here - there's no error in the web view, but when I console in and type 'Photo.all', I get nothing. What am I doing wrong? I'm kind of a newbie, please be gentle.
guitar.rb
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitar
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x6003'
}
end
guitars_controller.rb
class GuitarsController < ApplicationController
before_action :set_guitar, only: [:show, :edit, :update, :destroy]
# GET /guitars
# GET /guitars.json
def index
#guitars = Guitar.all
end
# GET /guitars/1
# GET /guitars/1.json
def show
end
# GET /guitars/new
def new
#guitar = current_user.guitars.build
3.times {#guitar.photos.build}
end
# GET /guitars/1/edit
def edit
3.times {#guitar.photos.build}
end
# POST /guitars
# POST /guitars.json
def create
#guitar = current_user.guitars.build(guitar_params)
respond_to do |format|
if #guitar.save
format.html { redirect_to #guitar, notice: 'Guitar was successfully created.' }
format.json { render action: 'show', status: :created, location: #guitar }
else
format.html { render action: 'new' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guitars/1
# PATCH/PUT /guitars/1.json
def update
respond_to do |format|
if #guitar.update(guitar_params)
format.html { redirect_to #guitar, notice: 'Guitar was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guitars/1
# DELETE /guitars/1.json
def destroy
#guitar.destroy
respond_to do |format|
format.html { redirect_to guitars_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guitar
#guitar = Guitar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :photos_attributes)
end
end
views/guitar/_form.html.erb
<%= form_for #guitar, :html => { :multipart => true } do |f| %>
<% if #guitar.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2>
<ul>
<% #guitar.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :make %><br>
<%= f.text_field :make %>
</div>
<div class="field">
<%= f.label :model %><br>
<%= f.text_field :model %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.text_field :year %>
</div>
<div class="field">
<%= f.label :color %><br>
<%= f.text_field :color %>
</div>
<div class="field">
<%= f.label :serial %><br>
<%= f.text_field :serial %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :condition %><br>
<%= f.number_field :condition %>
</div>
<div class="field">
<%= f.label :kind %><br>
<%= f.text_field :kind %>
</div>
<div class="field">
<%= f.label :bodykind %><br>
<%= f.text_field :bodykind %>
</div>
<div class="field">
<%= f.label :frets %><br>
<%= f.text_field :frets %>
</div>
<div class="field">
<%= f.label :oneowner %><br>
<%= f.check_box :oneowner %>
</div>
<div class="field">
<%= f.label :extended_description %><br>
<%= f.text_area :description %>
</div>
<%= f.fields_for :photos do |builder| %>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
db/schema.rb
ActiveRecord::Schema.define(version: 20131014195859) do
create_table "guitars", force: true do |t|
t.string "make"
t.string "model"
t.string "year"
t.string "color"
t.string "serial"
t.string "price"
t.integer "condition"
t.string "kind"
t.string "bodykind"
t.string "frets"
t.boolean "oneowner"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "description"
end
add_index "guitars", ["user_id"], name: "index_guitars_on_user_id", using: :btree
create_table "photos", force: true do |t|
t.integer "guitar_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "city"
t.string "state"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
There was a typo.
:photos_attributes => [:photo]
Replace existing code with following and try to run the app.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :photos_attributes [:photo])
end
Or you can check out the following link where i have created one sample app with nested attributes.
Nested Attributes Example

Resources