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]
Related
I used nested_form gem and I am trying to build a form which has fields from two tables(Project, Question).
My model:
class Project < ApplicationRecord
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ApplicationRecord
belongs_to :project
end
My controller:
class ProjectsController < ApplicationController
layout 'application'
def index
#projects = Project.all
end
def show
#project = Project.find(params[:id])
end
def new
#project = Project.new
#questions = #project.questions.build
end
def create
#project = Project.new(project_params)
#project.save
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
private
def project_params
params.require(:project).permit(:name, question_attributes: [:id, :content, :_delete])
end
end
My view:
<%= nested_form_for(#project) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |message| %>
<li>
<%= message %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :ff => builder %>
<% end %>
<p>
<%= f.link_to_add "Add a questions",:questions %>
</p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And my schema file :
create_table "projects", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "questions", force: :cascade do |t|
t.integer "project_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_questions_on_project_id", using: :btree
end
And _question_fields file is :
<p>
<%= ff.label :content, "Question" %>
<%= ff.text_area :content%>
<%= ff.link_to_remove "Remove this task"%>
</p>
Table Project will be saved but the table Question could not be saved.Why?
After change this line of code
def project_params
params.require(:project).permit(:name, questions_attributes: [:id, :content, :_delete])
end
I get the following error:
1 error prohibited this project from being saved:
Questions project must exist
I applied the change too ,but this time nothing could not be saved.
def project_params
params.require(:project).permit(:name, questions_attributes: [:id, :content, :project_id, :_delete])
end
The issue is with the project_params. As you have has_many :questions, the question_attributes should be changed to questions_attributes
def project_params
params.require(:project).permit(:name, questions_attributes: [:id, :content, :_delete])
end
Update:
Questions project must exist
Either permit project_id in questions_attributes
def project_params
params.require(:project).permit(:name, questions_attributes: [:id, :content, :project_id, :_delete])
end
or set optional: true on the association
class Question < ApplicationRecord
belongs_to :project, optional: true
end
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.
Hi I am trying to create a rails app,My app working fine locally but when I deployed it on Heroku its giving error.I have tried all other solutions on stackoverflow but not any working for me.
Rendered userdetails/new.html.erb within layouts/application (69.0ms)
2014-11-09T17:38:23.427665+00:00 app[web.1]: Started GET "/userdetails/new" for 113.193.105.11 at 2014-11-09 17:38:23 +0000
2014-11-09T17:38:23.511449+00:00 app[web.1]: Completed 500 Internal Server Error in 80ms
2014-11-09T17:38:23.513446+00:00 app[web.1]: app/views/userdetails/new.html.erb:4:in `_app_views_userdetails_new_html_erb__4090279191377710950_69952963663860'
2014-11-09T17:38:23.513443+00:00 app[web.1]: app/views/userdetails/_form.html.erb:24:in `block in _app_views_userdetails__form_html_erb___1876473002485377605_69952963666180'
2014-11-09T17:38:23.511258+00:00 app[web.1]: Rendered userdetails/_form.html.erb (67.7ms)
2014-11-09T17:38:23.430938+00:00 app[web.1]: Processing by UserdetailsController#new as HTML
2014-11-09T17:38:23.513448+00:00 app[web.1]:
2014-11-09T17:38:23.513426+00:00 app[web.1]:
2014-11-09T17:38:23.513429+00:00 app[web.1]: ActionView::Template::Error (undefined method `myuserid' for #<Userdetail:0x007f3e631929d8>):
2014-11-09T17:38:23.513430+00:00 app[web.1]: 21: </div>
2014-11-09T17:38:23.513433+00:00 app[web.1]: 22: <div class="field">
2014-11-09T17:38:23.513435+00:00 app[web.1]: 23: <%= f.label :myuserid , 'User ID'%><br>
2014-11-09T17:38:23.513436+00:00 app[web.1]: 24: <%= f.text_field :myuserid %>
2014-11-09T17:38:23.513438+00:00 app[web.1]: 25: </div>
2014-11-09T17:38:23.513439+00:00 app[web.1]: 26: <div class="field">
2014-11-09T17:38:23.513441+00:00 app[web.1]: 27: <%= f.label :birthday %><br>
2014-11-09T17:38:23.513445+00:00 app[web.1]: app/views/userdetails/_form.html.erb:1:in `_app_views_userdetails__form_html_erb___1876473002485377605_69952963666180'
My userdetail/new.html.erb looks like
<h1>New userdetail</h1>
<%= render 'form',userdetail: #userdetail %>
<%= link_to 'Back', userdetails_path %>
and My userdetail/_form.html looks like
<%= form_for(#userdetail) do |f| %>
<% if #userdetail.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#userdetail.errors.count, "error") %> prohibited this userdetail from being saved:</h2>
<ul>
<% #userdetail.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :myuserid , 'User ID'%><br>
<%= f.text_field :myuserid %>
</div>
<div class="field">
<%= f.label :birthday %><br>
<%= f.date_select :birthday %>
</div>
<div class="field">
<%= f.label :gender %><br>
<%= f.text_field :gender %>
</div>
<div class="field">
<%= f.label :contact_number %><br>
<%= f.text_field :contact_number %>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.text_field :country %>
</div>
<div class="field">
<%= f.label :state %><br>
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :gre_date %><br>
<%= f.date_select :gre_date %>
</div>
<%= f.hidden_field :user_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My userdetail controller looks like
class UserdetailsController < ApplicationController
before_action :set_userdetail, only: [:show, :edit, :update, :destroy]
#User can only edit or destroy its own details
before_action :correct_user, only: [:edit, :update, :destroy]
# GET /userdetails
# GET /userdetails.json
def index
#userdetails = Userdetail.all
end
# GET /userdetails/1
# GET /userdetails/1.json
def show
end
# GET /userdetails/new
def new
#userdetail = Userdetail.new
end
# GET /userdetails/1/edit
def edit
end
# POST /userdetails
# POST /userdetails.json
def create
#userdetail = Userdetail.new(userdetail_params)
#userdetail.user_id = current_user.id
respond_to do |format|
if #userdetail.save
format.html { redirect_to #userdetail, notice: 'Userdetail was successfully created.' }
format.json { render :show, status: :created, location: #userdetail }
else
format.html { render :new }
format.json { render json: #userdetail.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /userdetails/1
# PATCH/PUT /userdetails/1.json
def update
respond_to do |format|
if #userdetail.update(userdetail_params)
format.html { redirect_to #userdetail, notice: 'Userdetail was successfully updated.' }
format.json { render :show, status: :ok, location: #userdetail }
else
format.html { render :edit }
format.json { render json: #userdetail.errors, status: :unprocessable_entity }
end
end
end
# DELETE /userdetails/1
# DELETE /userdetails/1.json
def destroy
#userdetail.destroy
respond_to do |format|
format.html { redirect_to userdetails_url, notice: 'Userdetail was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_userdetail
#userdetail = Userdetail.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def userdetail_params
params.require(:userdetail).permit(:first_name, :last_name, :myuserid, :birthday, :gender, :contact_number, :address, :country, :state, :city, :gre_date)
end
def correct_user
#user = #userdetail.user_id
redirect_to( userdetails_url) unless #user == current_user.id
end
end
My user detail model looks like
class Userdetail < ActiveRecord::Base
belongs_to :user
#validates that one user can create just one user details
validates_uniqueness_of :user_id, :message => "details already exist"
end
My db schema looks like
ActiveRecord::Schema.define(version: 20141108175213) do
create_table "posts", force: true do |t|
t.text "issue", default: "", null: false
t.text "description", default: "", null: false
t.integer "rating"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "topic_id"
end
create_table "topics", force: true do |t|
t.text "issue", default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "userdetails", force: true do |t|
t.string "first_name"
t.string "last_name"
t.date "birthday"
t.string "gender"
t.string "contact_number"
t.text "address"
t.string "country"
t.string "state"
t.string "city"
t.date "gre_date"
t.datetime "created_at"
t.datetime "updated_at"
t.string "myuserid"
t.integer "user_id"
end
add_index "userdetails", ["myuserid"], name: "index_userdetails_on_myuserid", unique: true
add_index "userdetails", ["user_id"], name: "index_userdetails_on_user_id"
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"
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", unique: true
end
Please tell me answer. I generated app's view and model using command rails g scaffold userdetail but added 'myuserid' column later.Please see any if there is any other fault inn my code and suggest correction
Also my directory contain automatically generated duplicate files with ~ at the end of name, Any idea ?
I found the answer, when i ran rails console in heroku I found that myuserid column was not created in heroku. When i check my migration files I found that the migration file creating myuserid column was accidentally deleted. I recreated that file and my app start working.
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 %>
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