Rails Carrierwave Multiple Images Not Posting - ruby-on-rails

I'm working on a small project in Rails loosely following the Rails Tutorial. I have Carrierwave, Fog, and AWS working wonderfully on production for uploading a single image. I wanted to add a gallery of images, so I figured I would follow SSR's answer in the link: Rails 4 multiple image or file upload using carrierwave
Everything seems to work well for my model, and the tests even pass, but when I go to create or edit a listing, the uploader doesn't seem to work. It will hang for minutes without resolving (even on small file sizes), and the console doesn't read that an action was posted on submitting (just reads 200 for the last GET for the form itself). Other than naming conventions (I use Listing as the Post model and listing_gallery instead of post_attachment), the steps were identical. Did Rails 5 change anything about Carrierwave?
listing.rb
class Listing < ApplicationRecord
VALID_PHONE_REGEX = /(\(*(\d{3})\)|(\d{3}-)|(\d{3}))\s*(\d{3})-*(\d{4})/
VALID_ZIP_REGEX = /\d{5}((-)?\d{4})?/
# One use has many listings
belongs_to :user
# Listing_gallery
has_many :listing_galleries
accepts_nested_attributes_for :listing_galleries
# Descending order from newest to oldest as default
default_scope -> { order(created_at: :desc) }
# Carrier Wave mount uploader for images
mount_uploader :picture, PictureUploader
mount_uploader :image, PictureUploader
# Validation of parameters before accepting listing
validates :user_id, presence: true
validates :description, presence: true
validates :street_address, presence: true
validates :city, presence: true, length: { maximum: 30 }
validates :zip_code, presence: true, length: {minimum: 5, maximum: 10}, format: { with: VALID_ZIP_REGEX }
validates :primary_contact, presence: true, length: {minimum: 10, maximum: 15}, format: { with: VALID_PHONE_REGEX }
end
listings_controller.rb
class ListingsController < ApplicationController
before_action :logged_in_user, only: [:new, :create, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def new
#listing = Listing.new
#listing_gallery = #listing.listing_galleries.build
end
def create
if current_user.admin?
#listing = current_user.listings.build(listing_params)
#listing.listing_status = "active"
respond_to do |format|
if#listing.save!
params[:listing_galleries]['image'].each do |image|
#listing_gallery = #listing.listing_galleries.create!(:image => image)
end
flash[:success] = "New Listing created!"
redirect_to root_url
else
render 'new'
end
end
else
flash[:failure] = "You must be an admin to create a listing."
redirect_to root_url
end
end
def show
#listing = Listing.find(params[:id])
#listing_galleries = #listing.listing_galleries.all
end
def edit
if current_user.admin?
#listing = Listing.find(params[:id])
render 'edit'
end
end
def update
if current_user.admin?
#listing = Listing.find(params[:id])
respond_to do |format|
if #listing.update_attributes(listing_params)
if params[:listing_galleries] != []
params[:listing_galleries]['image'].each do |image|
#listing_gallery = #listing.listing_galleries.create!(:image => image)
end
end
flash[:success] = "Listing Updated!"
redirect_to #listing
else
render 'edit'
end
end
end
end
def destroy
if current_user.admin?
#listing.listing_status = "deleted"
if #listing.save!
flash[:success] = "Listing DELETED"
redirect_to request.referrer || root_url
else
flash[:failure] = "Could not remove listing"
redirect_to request.referrer || root_url
end
end
end
private
# Check for necessary paramters
def listing_params
params.require(:listing).permit(:description, :street_address, :city, :state,
:zip_code, :primary_contact, :secondary_contact,
:listing_status, :asking_price, :renobb, :picture,
{ image: [] },
listing_galleries_attributes: [:id, :listing_id, :image, :_destroy])
end
# Ensure only the creator of the listing is destroying it
def correct_user
#listing = current_user.listings.find_by(id: params[:id])
if current_user.admin?
return true
end
redirect_to root_url if #listing.nil?
end
end
listings/new.html.erb
<% provide(:title, 'Create Listing') %>
<div id="content">
<div id="sectionbanner">
<p class="goldtext">
Create a New Listing
</p>
</div>
<!-- Article -->
<div id="article">
<div class="form">
<%= form_for(#listing, html: { multipart: true}) do |form| %>
<%= render 'shared/error_messages_listings' %>
<%= form.label :street_address %>
<br>
<%= form.text_field :street_address, class: 'form-control' %>
<br>
<%= form.label :city %>
<br>
<%= form.text_field :city, class: 'form-control' %>
<br>
<%= form.label :state %>
<br>
<%= form.text_field :state, class: 'form-control' %>
<br>
<%= form.label :zip_code %>
<br>
<%= form.text_field :zip_code, class: 'form-control' %>
<br>
<%= form.label :primary_contact %>
<br>
<%= form.text_field :primary_contact, class: 'form-control' %>
<br>
<%= form.label :secondary_contact %>
<br>
<%= form.text_field :secondary_contact, class: 'form-control' %>
<br>
<%= form.label :asking_price %>
<br>
<%= form.number_field :asking_price, class: 'form-control' %>
<br>
<%= form.label :description %>
<br>
<%= form.text_area :description, placeholder: "Details of the lot, rules, etc..." %>
<br>
<%= form.label :renobb %>
<br>
<%= form.check_box :renobb %>
<br>
<span class="picture">
<%= form.file_field :picture, accept: 'image/jpeg, image/gif, image/png' %>
</span>
<br>
<%= form.label :image %>
<br>
<%= form.file_field :image, :multiple => true, name: "listing_galleries[image][]" %>
<br>
<%= form.submit "Create Listing", class: "button" %>
<% end %>
</div>
</div>
</div>
listing_gallery.rb
class ListingGallery < ApplicationRecord
belongs_to :listing
mount_uploader :image, PictureUploader
end
listing_galleries_controller.rb
class ListingGalleriesController < ApplicationController
before_action :set_listing_gallery, only: [:show, :edit, :update, :destroy]
# GET /listing_galleries
# GET /listing_galleries.json
def index
#listing_galleries = ListingGallery.all
end
# GET /listing_galleries/1
# GET /listing_galleries/1.json
def show
end
# GET /listing_galleries/new
def new
#listing_gallery = ListingGallery.new
end
# GET /listing_galleries/1/edit
def edit
end
# POST /listing_galleries
# POST /listing_galleries.json
def create
#listing_gallery = ListingGallery.new(listing_gallery_params)
respond_to do |format|
if current_user.admin?
if #listing_gallery.save
format.html { redirect_to #listing_gallery, notice: 'Listing gallery was successfully created.' }
format.json { render :show, status: :created, location: #listing_gallery }
else
format.html { render :new }
format.json { render json: #listing_gallery.errors, status: :unprocessable_entity }
end
end
end
end
# PATCH/PUT /listing_galleries/1
# PATCH/PUT /listing_galleries/1.json
def update
respond_to do |format|
if current_user.admin?
if #listing_gallery.update(listing_gallery_params)
format.html { redirect_to #listing_gallery, notice: 'Listing gallery was successfully updated.' }
format.json { render :show, status: :ok, location: #listing_gallery }
else
format.html { render :edit }
format.json { render json: #listing_gallery.errors, status: :unprocessable_entity }
end
end
end
end
# DELETE /listing_galleries/1
# DELETE /listing_galleries/1.json
def destroy
if current_user.admin?
#listing_gallery.destroy
respond_to do |format|
format.html { redirect_to listing_galleries_url, notice: 'Listing gallery was successfully destroyed.' }
format.json { head :no_content }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing_gallery
#listing_gallery = ListingGallery.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_gallery_params
params.require(:listing_gallery).permit(:listing_id, :image, { image: [] }, :_destroy)
end
end
EDIT More files from the controllers and views. (Alignment may be funky on SO, but it's alright in project.)

For multiple image upload you need to add this gem to your gemfile
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
and permit the params as an open array in your controller: {image: []}
Check gem documentation for more details
https://github.com/carrierwaveuploader/carrierwave
You also seem to have an uploader 'image' which you are not mounting in your model. You only mount 'picture'.

I really don't know if this is a typo, but your problem is on line 1 of your view.
%= form_for(#listing, html: { mulipart: true}) do |form| %>
You are also missing the spelling of Multipart which I suppose is the reason you couldn't upload multiple pictures.
Change it to:
<%= form_for(#listing, html: { multipart: true}) do |form| %>
For more insight, refer to Multiple File Uploads section of Carrierwave Documentation.

Related

Creating a dynamic drop down menu raising a Load Error in my controller

I am updating my code to move from static drop down menus to dynamic related drop down menus. I am following some instructions from an online tutorial adapting to fit my needs but have become unstuck . The tutorial I am following is here....https://rubyplus.com/articles/3691-Dynamic-Select-Menus-in-Rails-5
LoadError (Unable to autoload constant Job_category, expected C:/Users/User/Desktop/ServeandProtect/app/models/job_category.rb to define it):
app/views/jobs/listing.html.erb:22:in block in _app_views_jobs_listing_html_erb__816863472_104736972'
app/views/jobs/listing.html.erb:16:in_app_views_jobs_listing_html_erb__816863472_104736972'
** My View/HTML - first drop down has been updated to use models **
<div class="row">
<div class="col-md-3">
<%= render 'job_menu' %>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
Listing
</div>
<div class="panel-body">
<div class="container">
<%= form_for #job do |f| %>
<div class="row">
<div class="col-md-4 select">
<div class="form-group">
<%= f.label :job_category_id %><br />
<%= f.collection_select :job_category_id, Job_category.order(:job_category),
:id, :job_category, include_blank: true %>
</div>
</div>
<div class="col-md-4 select">
<div class="form-group">
<label>Job Type</label>
<%= f.select :job_type, [["Cleaning", "Cleaning"], ["Gardening", "Gardening"],
["Rubbish Clearance", "Rubbish Clearance"], ["Removals", "Removals"],
["Collection & Delivery", "Collection & Delivery"], ["Car Service & Repairs", "Car Service & Repairs"]],
id: "job_type", prompt: "Select...", class: "form-control" %>
</div>
</div>
<div class="col-md-4 select">
<div class="form-group">
<label>Frequency</label>
<%= f.select :recurrence, [["One Off", "One Off"], ["Daily", "Daily"],
["Weekly", "Weekly"], ["Bi-Monthly", "Bi-Monthly"],
["Once-Monthly", "Once-Monthly"]],
id: "recurrence", prompt: "Select...", class: "form-control" %>
</div>
</div>
</div>
<div><%= f.submit "Save", class: "btn btn-normal" %></div>
<% end %>
</div>
</div>
</div>
</div>
</div>
** My Controller **
class JobsController < ApplicationController
before_action :set_job , except: [:index, :new, :create, :edit, :delete_image_attachment ]
before_action :authenticate_user!, except: [:show]
before_action :is_authorised, only: [:listing, :budget, :description, :photo_upload, :location,
:update, :show, :delete, :featured, :premium ]
# GET /jobs
def index
#jobs = Job.all
end
# GET /jobs/1
def show
end
# GET /jobs/new
def new
#job = current_user.jobs.build
end
def listing
##job = current_user.job
end
# GET /jobs/1/edit
def edit
end
def budget
end
# POST /jobs
def create
#job = current_user.jobs.build(job_params)
if #job.save!
redirect_to listing_job_path(#job), notice: 'Jobs dev was successfully created.'
else
render :new
end
end
# PATCH/PUT /jobs/1
# def update
# if #job.update(job_params)
# redirect_to #job, notice: 'Jobs dev was successfully updated.'
# else
# render :edit
# end
# end
def update
#respond_to do |format|
if #job.update(job_params)
#format.html { redirect_to #job, notice: 'Post was successfully updated.' }
#format.json { render :show, status: :ok, location: #job }
flash[:notice] = 'Saved...'
else
#format.html { render :edit }
#format.json { render json: #job.errors, status: :unprocessable_entity }
flash[:alert] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
#end
end
# DELETE /jobs/1
def destroy
#job.destroy
redirect_to jobs_url, notice: 'Job was successfully destroyed.'
end
def delete
end
def featured
##job = current_user.job
end
def premium
##job = current_user.job
end
# DELETE job images
def delete_image_attachment
#image = ActiveStorage::Attachment.find(params[:image_id])
#image.purge
redirect_back(fallback_location: request.referer)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
#job = Job.find(params[:id])
end
def is_authorised
redirect_to root_path, alert: "You don't have permission..." unless current_user.id == #job.user_id
end
# Only allow a trusted parameter "white list" through.
def job_params
params.require(:job).permit(:job_category, :job_type, :job_title, :job_description, :recurrence,
:featured, :premium, :budget, images: []
)
end
end
** My Models **
[![class JobCategory < ApplicationRecord
#attr_accessible :job_category
#validates :job_category, :presence => true
has_many :job_types
has_many :jobs
end
class JobType < ApplicationRecord
#attr_accessible :job_type
#validates :job_type, :presence => true
belongs_to :job_category
has_many :jobs
end
class Job < ApplicationRecord
belongs_to :user
#belongs_to :job_category
#belongs_to :job_type
has_many_attached :images
validates :job_category, presence: true
validates :job_type, presence: true
validates :recurrence, presence: true
#validates :job_title, presence: true
#validates :job_description, presence: true
end][1]][1]
** The Error **
Your model is probably not named Job_category but JobCategory. Just change your model name in your view from
<%= f.collection_select :job_category_id, Job_category.order(:job_category),
:id, :job_category, include_blank: true %>
to
<%= f.collection_select :job_category_id, JobCategory.order(:job_category),
:id, :job_category, include_blank: true %>

Rails 'required: true' does not work fully

I have a simple_form with a grouped collection select and two input fields. I have a required: true on both fields, but it still allows empty input through. The little 'required' asterisk appears next to the field name, but that's it. Is there any way I can prevent empty input from going through the form?
new.rb
<h1>New Article</h1>
<%= render 'form', article: #article %>
<%= link_to 'Back', articles_path(category_id: params[:category_id]) %>
_form.rb
<%= simple_form_for(article, html: {class: 'form-vertical'}) do |f| %>
<% if article.errors.any? %>
<div id="error_explanation">
<h4><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h4>
<ul>
<% article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%# field being selected, parent collection, subcollection, displayed, key, value %>
<%= f.grouped_collection_select :subcategory_id, Category.all,:subcategories,:name, :id,:name, {required: true} %>
<%= f.input :title, required: true %>
<%= f.input :content, input_html: { rows: 20 }, required: true%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# GET /articles
# GET /articles.json
def index
if params[:category_id].blank? && params[:subcategory_id].blank?
#articles = Article.all.order("created_at DESC")
elsif params[:subcategory_id].blank?
#articles = Article.where(category_id: params[:category_id])
else
#articles = Article.where(subcategory_id: params[:subcategory_id]).order("created_at DESC")
end
end
# GET /articles/1
# GET /articles/1.json
def show
end
# GET /articles/new
def new
#article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
#parameters = article_params
#parameters[:category_id] = Subcategory.find(#parameters[:subcategory_id]).category_id
#article = Article.new(#parameters)
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: #article }
else
format.html { render :new }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1
# PATCH/PUT /articles/1.json
def update
respond_to do |format|
if #article.update(article_params)
format.html { redirect_to #article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: #article }
else
format.html { render :edit }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
#article.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Article was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
#article = Article.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:title,:content,:subcategory_id)
end
end
As per simple documentation of rails. Your model should look like this(suppose i wanted to validate presence of first_name last_name and compnay_name):
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :company_name, presence: true
validates :first_name, presence: true
validates :last_name, presence: true
end
Now after this you are ready to go, presence: true will validate data before saving it to the database. A good practice is to add a validation to the front end too. which is add require: true to your form_for helper.
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: true, required: true ,autocomplete: "email", class: "border p-3 w-100 my-2" %>
</div>
You need to use validations, that logic is done inside the model.
Make sure you read and undertand the ActiveRecord Validations Guide because it will save you a lot of time.
Example taken from the guide:
class Person < ApplicationRecord
validates :name, presence: true
end
Person.create(name: "John Doe").valid? # => true
Person.create(name: nil).valid? # => false
Basically when the object is saved, it runs the validations and if it's not valid it will fill the errors variable with the messages.
Simple form documentation states...
By default all inputs are required. When the form object has presence
validations attached to its fields, Simple Form tells required and
optional fields apart. For performance reasons, this detection is
skipped on validations that make use of conditional options, such as
:if and :unless.
Which means that all you need to do is add into your model/article.rb file
class Article < ActiveRecord::Base
validates :title, presence: true
validates :content, presence: true

Nested form working in new but not showing up in edit Rails

I'm making a invoice application where i have the #invoice form and inside it i have nested forms for customers, products, and company information. The products form is working fine in all views but the customer form isn't. When i fill in the customer information and create a new invoice it works. But when i try to edit that invoice the entire form is gone.
invoice/_form
<%= form_for #invoice do |f| %>
<% if #invoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
<ul>
<% #invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :customer do |customer| %>
<div class="field">
<%= customer.label 'Bedrijfsnaam ontvanger' %><br/>
<%= customer.text_field :company_name, placeholder: 'bedrijfsnaam', class: 'form-control' %>
</div>
<div class="field">
<%= customer.label 'Adres ontvanger' %><br>
<%= customer.text_field :address_line_1, placeholder: 'adres ontvanger', class: 'form-control' %>
</div>
<div class="field">
<%= customer.label 'Postcode & stad' %><br>
<%= customer.text_field :zip_code, placeholder: '1234AB Rotterdam', class: 'form-control' %>
</div>
<% end %>
Invoices_controller.rb
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
#invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
#invoice = Invoice.new
#invoice.products.build
#invoice.build_customer
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
#invoice = Invoice.new(invoice_params)
respond_to do |format|
if #invoice.save
format.html { redirect_to #invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: #invoice }
else
format.html { render :new }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if #invoice.update(invoice_params)
format.html { redirect_to #invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
#invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
#invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal,
:subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy],
companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy],
products_attributes: [:id, :quantity, :description, :unitprice, :btw, :total])
end
end
Invoice.rb - (model)
class Invoice < ActiveRecord::Base
has_one :company
has_one :customer
has_many :products
accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
In the invoices controller try changing customers_attributes to customer_attributes in your strong params:
customer_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy]
I suspect this is an issue where your customer nested attributes are not being properly saved, so when you go to the edit view that part of the form isn't being rendered because there isn't any customer saved for your invoice

Nested attributes in Ruby on Rails not saving

I'll preface this by saying I've looked at the following answers and still have not hit a solution that works (however, given the possibility I might have overlooked something, I'm including them for reference):
Ruby on Rails - Not saving User - Nested Attributes and Nested Forms
Nested Attributes in ruby on rails
Ruby on Rails nested attributes not saving into database?
Nested attributes not saving in database? Does not display values - Ruby on Rails
Description of problem: I have a form Block with a nested form for Cue. The form renders correctly and the Block saves correctly, but the Cue does not appear in the Cue table, i.e. the Cue isn't saving when the Block is submitted. I'm using Rails 4.2.5.1. I also don't get any errors on submit, making this a bit difficult to diagnose.
Code:
_form.html.erb - block
<%= form_for(#block) do |f| %>
<% if #block.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#block.errors.count, "error") %> prohibited this block from being saved:</h2>
<ul>
<% #block.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field hidden">
<%= f.label :block_code, class: "hidden" %><br>
<%= f.text_field :block_code, class: "form-control hidden" %>
</div>
<div class="field">
<%= f.label :block_duration %><br>
<div class="input-group">
<%= f.number_field :block_duration, class: 'text_field form-control', :step => 'any' %>
<div class="input-group-addon">seconds</div>
</div>
</div>
<div class="field">
<label>Does this block have a cue associated with it?</label>
<input type="radio" name="cue" value="cueYes" data-type="cueYes" data-radio="cue"> Yes
<input type="radio" name="cue" value="cueNo" data-type="cueNo" data-radio="cue" checked> No
<div class="field" id="cueYes">
<%= f.fields_for :cues do |ff| %>
<div class="field hidden">
<%= ff.label :cue_code, class: "hidden" %><br>
<%= ff.text_field :cue_code, class: "hidden" %>
</div>
<div class="field">
<%= ff.label "Cue Type" %><br>
<%= ff.collection_select(:cue_type_code, CueType.all, :cue_type_code, :cue_type_name, {prompt: "Select a cue type..."}, {class: "form-control"}) %>
</div>
<div class="field">
<%= ff.label "Cue Description" %><br>
<%= ff.text_area :cue_description, class: "form-control" %>
</div>
<div class="field">
<%= ff.label "Cue Method" %><br>
<%= ff.collection_select( :cue_method_code, CueMethod.all, :cue_method_code, :cue_method_name, {prompt: "Select a cue method..."}, {class: "form-control"}) %>
</div>
<% end %>
</div>
</div>
<div class="field">
<%= f.label "Location" %><br>
<%= collection_select :block, :location_code, Location.all, :location_code, :location_name, {prompt: "Select a location..."}, {class: "form-control"} %>
</div>
<div class="field">
<%= f.label "Scene" %><br>
<%= collection_select :block, :scene_code, Scene.all, :scene_code, :actAndScene, {prompt: "Select a scene..."}, {class: "form-control"} %>
</div>
<div class="field">
<%= f.label "Block Description" %><br>
<%= f.text_area :block_description, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Create Block", class: "btn btn-primary" %>
</div>
<% end %>
blocks_controller.rb
class BlocksController < ApplicationController
before_action :set_block, only: [:show, :edit, :update, :destroy]
# GET /blocks
# GET /blocks.json
def index
#blocks = Block.all
end
# GET /blocks/1
# GET /blocks/1.json
def show
end
# GET /blocks/new
def new
#block = Block.new
# Set block code as next integer after max block code.
#block.block_code = (Block.maximum(:block_code).to_i.next).to_s(2)
end
# GET /blocks/1/edit
def edit
end
# POST /blocks
# POST /blocks.json
def create
#block = Block.new(block_params)
respond_to do |format|
if #block.save
format.html { redirect_to #block, notice: 'Block was successfully created.' }
format.json { render :show, status: :created, location: #block }
else
format.html { render :new }
format.json { render json: #block.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /blocks/1
# PATCH/PUT /blocks/1.json
def update
respond_to do |format|
if #block.update(block_params)
format.html { redirect_to #block, notice: 'Block was successfully updated.' }
format.json { render :show, status: :ok, location: #block }
else
format.html { render :edit }
format.json { render json: #block.errors, status: :unprocessable_entity }
end
end
end
# DELETE /blocks/1
# DELETE /blocks/1.json
def destroy
#block.destroy
respond_to do |format|
format.html { redirect_to blocks_url, notice: 'Block was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_block
#block = Block.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def block_params
params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cues_attributes => [:cue_code, :cue_type_code, :cue_description, :cue_method_name])
end
end
block.rb
class Block < ActiveRecord::Base
has_one :cue, -> { where processed: true }
accepts_nested_attributes_for :cue, allow_destroy: true
end
cue.rb
class Cue < ActiveRecord::Base
belongs_to :block
end
cues_controller.rb
class CuesController < ApplicationController
before_action :set_cue, only: [:show, :edit, :update, :destroy]
# GET /cues
# GET /cues.json
def index
#cues = Cue.all
end
# GET /cues/1
# GET /cues/1.json
def show
end
# GET /cues/new
def new
#cue = Cue.new
# Set cue code as next integer after max cue code.
#cue.cue_code = (Cue.maximum(:cue_code).to_i.next).to_s(2)
end
# GET /cues/1/edit
def edit
end
# POST /cues
# POST /cues.json
def create
#cue = Cue.new(cue_params)
respond_to do |format|
if #cue.save
format.html { redirect_to #cue, notice: 'Cue was successfully created.' }
format.json { render :show, status: :created, location: #cue }
else
format.html { render :new }
format.json { render json: #cue.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cues/1
# PATCH/PUT /cues/1.json
def update
respond_to do |format|
if #cue.update(cue_params)
format.html { redirect_to #cue, notice: 'Cue was successfully updated.' }
format.json { render :show, status: :ok, location: #cue }
else
format.html { render :edit }
format.json { render json: #cue.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cues/1
# DELETE /cues/1.json
def destroy
#cue.destroy
respond_to do |format|
format.html { redirect_to cues_url, notice: 'Cue was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cue
#cue = Cue.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cue_params
params.require(:cue).permit(:cue_code, :cue_type_code, :cue_description, :cue_method_code)
end
end
If anything else is needed, please let me know! (Also sorry if the formatting isn't great).
Any help is much appreciated!! Thanks!!
UPDATE 1
I'm currently getting the error undefined method 'encoding' for 7:Fixnum on the line if #block.save in blocks_controller.rb (above). I have changed the following things based on the answer given by IngoAlbers (below) and the answer found here.
I've changed the following things:
blocks_controller.rb
def block_params
params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_code])
end
_form.html.erb - blocks
<%= f.fields_for :cue, #block.build_cue do |ff| %>
block.rb
class Block < ActiveRecord::Base
has_one :cue
accepts_nested_attributes_for :cue, allow_destroy: true
end
Thanks so much for the help so far! I think I'm really close!
UPDATE 2
So I've added block_id as an attribute to Cue and run the following two migrations:
class AddBlockIdToCues < ActiveRecord::Migration
def self.up
add_column :cues, :block_id, :binary
end
def self.down
remove_column :cues, :block_id
end
end
class AddBelongsToToCues < ActiveRecord::Migration
def change
add_reference :cues, :blocks, index: true
add_foreign_key :cues, :blocks
end
end
I'm still getting the error undefined method 'encoding' for 7:Fixnum on the line if #block.save in the blocks_controller.rb.
The problem should be in your fields_for. It should probably be:
<%= f.fields_for :cue do |ff| %>
Not cues since it is only one. Then you need to build the cue. This can be done either in controller or in the view directly, for example like this:
<%= f.fields_for :cue, #block.build_cue do |ff| %>
In your block params you then also need to change it to cue_attributes, and also allow id.
def block_params
params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_name])
end
You can also read a lot more information here:
http://guides.rubyonrails.org/form_helpers.html#nested-forms
Regarding your second Update:
Your first migration adds a column block_id of type binary. It should definitely be integer instead. That said, you don't even need the first migration at all, because your second migration will handle all of it correctly, if you change blocks to block in add_reference. It should look like this:
class AddBelongsToToCues < ActiveRecord::Migration
def change
add_reference :cues, :block, index: true
add_foreign_key :cues, :blocks
end
end
The add_reference needs to add a reference to one block not multiple. This will then create the right column for you.
See also: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference

undefined method `facilities' for nil:NilClass - Nested actions

I have an "undefined method" issue with my app, and don't find where it comes from :(.
In my app, i have 4 models :
Deal, Pool (which belongs to deal), Facility (which belongs to pool), Facilityschedule (which belongs to Facility).
class Deal < ActiveRecord::Base
has_many :pools, :dependent => :destroy
accepts_nested_attributes_for :pools, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end
class Pool < ActiveRecord::Base
belongs_to :deal
has_many :facilities, :dependent => :destroy
accepts_nested_attributes_for :facilities, :allow_destroy => true
end
class Facility < ActiveRecord::Base
belongs_to :pool
has_many :facilityschedules, :dependent => :destroy
accepts_nested_attributes_for :facilityschedules, :reject_if => lambda { |a| a[:date].blank? }, :allow_destroy => true
end
class Facilityschedule < ActiveRecord::Base
belongs_to :facility
end
I have a partial form which allows the user to create all of these :
<%= form_for(#deal) do |f| %>
<% if #deal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#deal.errors.count, "error") %> prohibited this deal from being saved:</h2>
<ul>
<% #deal.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name, "Deal name"%>
<%= f.text_field :name %>
<br/>
</div>
<div>
<%= f.fields_for :pools do |builder|%>
<%= builder.label :name, "Pool name" %>
<%= builder.text_field :name, :rows => 3 %>
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Pool" %>
<br/>
<%= builder.fields_for :facilities do |fbuilder|%>
<%= fbuilder.label :name, "Facility name" %>
<%= fbuilder.text_field :name, :rows => 3 %>
<%= fbuilder.check_box :_destroy %>
<%= fbuilder.label :_destroy, "Remove Facility" %>
<br/>
<%= fbuilder.fields_for :facilitieschedules do |sbuilder|%>
<%= sbuilder.label :date, "Schedule" %>
<%= sbuilder.text_field :date, :rows => 3 %>
<%= sbuilder.check_box :_destroy %>
<%= sbuilder.label :_destroy, "Remove Schedule" %>
<br/>
</div>
<% end %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And finally, i have my Deal controller where the issue is located (new action) :
class DealsController < ApplicationController
before_action :set_deal, only: [:show, :edit, :update, :destroy]
# GET /deals
# GET /deals.json
def index
#deals = Deal.all
end
# GET /deals/1
# GET /deals/1.json
def show
end
# GET /deals/new
def new
#deal = Deal.new
2.times do
pool = #deal.pools.build
2.times do
**facility = #pool.facilities.build**
1. times { facility.facilityschedules.build }
end
end
end
# GET /deals/1/edit
def edit
end
# POST /deals
# POST /deals.json
def create
#deal = Deal.new(deal_params)
respond_to do |format|
if #deal.save
format.html { redirect_to #deal, notice: 'Deal was successfully created.' }
format.json { render :show, status: :created, location: #deal }
else
format.html { render :new }
format.json { render json: #deal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /deals/1
# PATCH/PUT /deals/1.json
def update
respond_to do |format|
if #deal.update(deal_params)
format.html { redirect_to #deal, notice: 'Deal was successfully updated.' }
format.json { render :show, status: :ok, location: #deal }
else
format.html { render :edit }
format.json { render json: #deal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /deals/1
# DELETE /deals/1.json
def destroy
#deal.destroy
respond_to do |format|
format.html { redirect_to deals_url, notice: 'Deal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_deal
#deal = Deal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def deal_params
params.require(:deal).permit(:name, pools_attributes: [:id, :name, :number, :deal_id, :_destroy, facilities_attributes: [:id, :name, :pool_id, :_destroy, facilityschedules_attributes: [:id, :facility_id, :date, :_destroy]]])
end
end
When i try to create a new deal, the following error message pops up "undefined method `facilities' for nil:NilClass" (in bold in the Deal controller, above).
What am i doing wrong?
Many thanks and have a good week end :)
# GET /deals/new
def new
#deal = Deal.new
2.times do
pool = #deal.pools.build
2.times do
**facility = #pool.facilities.build**
1. times { facility.facilityschedules.build }
end
end
end
In the code above you have never set the #pool variable, but instead set pool.
Undefined method for Nil::NilClass is never about not having declared the method, it's that you're trying to call it on a nil object, which is an object of NilClass.
There are also some strange patterns in this method. I've never seen the ** around code in Rails. Are you trying to comment out the line?
Would this work better?
# GET /deals/new
def new
#deal = Deal.new
2.times do
#pool = #deal.pools.build
2.times do
facility = #pool.facilities.build
facility.facilityschedules.build
end
end
end

Resources