undefined method `map' error on should create model test - ruby-on-rails

app/controllers/products_controller.rb:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /products
# GET /products.json
def index
##products = Product.all
#products = Product.all
if params[:search]
#products = Product.search(params[:search]).order("created_at DESC").paginate(page: params[:page], per_page: 5)
else
#products = Product.all.order('created_at DESC').paginate(page: params[:page], per_page: 5)
end
if session[:cart] then
#cart = session[:cart]
else
#cart = {}
end
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
if current_user.admin?
##product = Product.new
#product = Product.new
#categories = Category.all.map{|c| [ c.name, c.id ] }
end
end
# GET /products/1/edit
def edit
if current_user.admin?
#categories = Category.all.map{|c| [ c.name, c.id ] }
end
end
# POST /products
# POST /products.json
def create
if current_user.admin?
#product = Product.new(product_params)
#product.category_id = params[:category_id]
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
if current_user.admin?
#product.category_id = params[:category_id]
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
if current_user.admin?
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :photo, :price, :category, :subcategory)
end
end
app/views/products/new.html.erb:
<h1>New Product</h1>
<%= render 'form' %>
<%= link_to 'Back', products_path %>
app/views/products/_form.html.erb:
<%= form_for(#product, multipart: true) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :photo %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.number_field :price, :step => "0.01" %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= select_tag(:category_id, options_for_select(#categories), :prompt => "Select one!") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My test in test/controllers/products_controller_test.rb:
test "should create product" do
sign_in users(:admin)
assert_difference('Product.count') do
post :create, product: { category_id: #product.category, description: #product.description, photo_content_type: #product.photo_content_type, photo_file_name: #product.photo_file_name, photo_file_size: #product.photo_file_size, photo_updated_at: #product.photo_updated_at, price: #product.price, title: #product.title }
end
assert_redirected_to product_path(assigns(:product))
end
Causing this error:
Finished in 0.816541s, 24.4936 runs/s, 35.5157 assertions/s.
1) Error:
ProductsControllerTest#test_should_create_product:
ActionView::Template::Error: undefined method `map' for nil:NilClass
app/views/products/_form.html.erb:32:in `block in _app_views_products__form_html_erb__4489093195482743578_70189257075560'
app/views/products/_form.html.erb:1:in `_app_views_products__form_html_erb__4489093195482743578_70189257075560'
app/views/products/new.html.erb:3:in `_app_views_products_new_html_erb__3438187527367239596_70189257283820'
app/controllers/products_controller.rb:57:in `block (2 levels) in create'
app/controllers/products_controller.rb:52:in `create'
test/controllers/products_controller_test.rb:29:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:28:in `block in <class:ProductsControllerTest>'
20 runs, 29 assertions, 0 failures, 1 errors, 0 skips
I'm running my tests with rake test:functionals. The other tests are fine but only this test is problematic.
There is even no any method named map in app/views/products/_form.html.erb:. So I don't have any idea for cause of this error.
If you want to look to the whole project: https://github.com/mertyildiran/SCOR
CHANGES:
According to answers:
In form I made this change: <%= f.collection_select(:category_id, Category.all, :id, :name) %>
In products controller: params.require(:product).permit(:title, :description, :photo, :price, :category_id)
And new version of test:
test "should create product" do
sign_in users(:admin)
image = fixture_file_upload "../../public/assets/products/1/original/" + #product.photo_file_name
puts image.inspect
assert_difference('Product.count') do
post :create, product: { category_id: #product.category, description: #product.description, photo: image, price: #product.price, title: #product.title }
end
assert_redirected_to product_path(assigns(:product))
end
We get rid of error but Failure persist, stdout:
# Running:
.......#<Rack::Test::UploadedFile:0x007f5a1894fdf8 #content_type=nil, #original_filename="ra_unisex_tshirt_x1000_fafafa-ca443f4786_front-c_235_200_225_294-bg_f8f8f8_(7).jpg", #tempfile=#<Tempfile:/tmp/ra_unisex_tshirt_x1000_fafafa-ca443f4786_front-c_235_200_225_294-bg_f8f8f8_(7).jpg20160515-31919-1j7oypa>>
F............
Finished in 0.800235s, 24.9926 runs/s, 37.4890 assertions/s.
1) Failure:
ProductsControllerTest#test_should_create_product [/home/mertyildiran/Documents/SCOR/test/controllers/products_controller_test.rb:25]:
"Product.count" didn't change by 1.
Expected: 3
Actual: 2
20 runs, 30 assertions, 1 failures, 0 errors, 0 skips
Validation Failure Cause:
<div id="error_explanation">
<h2>2 errors prohibited this product from being saved:</h2>
<ul>
<li>Photo content type is invalid</li>
<li>Photo is invalid</li>
</ul>
</div>
Fixed with this image.content_type = #product.photo_content_type

When you call post :create, product: ... in your test a validation is failing which causes the new view to be rendered.
The new view then causes an error by calling options_for_select(#categories) but #categories is not defined - in other words nil. options_for_select expects the argument to be an array and calls .map on nil.
Thats not the only problem - you are not nesting the input properly either.
<%= select_tag(:category_id, options_for_select(#categories), :prompt => "Select one!") %>
Would end up in params[:category_id] not params[:product][:category_id].
Instead what you want to do is use collection_select and bind it to the model:
<div class="field">
<%= f.label :category_id %><br>
<%= f.collection_select(:category_id, Category.all, :id, :name) %>
</div>
You should also ensure that you are whitelisting the correct param - and if you have a hard time keeping track of them make sure you test it as well!
def product_params
params.require(:product).permit(:title, :description, :photo, :price, :category_id, :subcategory)
end
If you want to debug exactly what attribute causes the validation failure there are a few options:
assigns(:product) will give you the #product instance variable from your controller. assert_equals(#product.errors.full_messages, []) gives you a list. You have to call the controller action first!
You can do assert_equals(#response.body, '') to get a dump of the page HTML. You have to call the controller action first!
Keep a tab on the logs with $ tail -f logs/test.log

From the stack trace it appears that the validation failed when trying to save the #product and the controller wants to render the new template. But you don't have the #categories variable defined for this scenario, that is probably where the map call for nil is getting from.
So, probably you should do both of the following:
add the #categories array to the failed save part of your create and update actions in your controller so that the failed-save scenario works ok
fix the test code so that the validations do not fail when creating the product.

Related

Cannot fill out price field in my editing section using Ruby on Rails

I have just recently tried to implement 'Stripe' to take payments on my website. However, I am have a certain issue trying to update the price on the products.
This is my payments controller.
class PaymentsController < ApplicationController
def create
token = params[:stripeToken]
#product = Product.find(params[:product_id])
#user = current_user
# Create the charge on Stripe's servers to charge the user's card
begin
charge = Stripe::Charge.create(
amount: (#product.price*100).to_i,
currency: "eur",
source: token,
description: params[:stripeEmail]
)
if charge.paid
Order.create!(product_id: #product.id, user_id: #user.id, total: #product.price.to_i)
UserMailer.order_placed(#user,#product).deliver_now
end
rescue Stripe::CardError => e
body = e.json_body
err = body[:error]
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
end
redirect_to product_path(#product), notice: 'Thank you for your order!'
end
end
My products model
class Product < ApplicationRecord
has_many :orders
has_many :comments
validates :name, presence: true
validates :price, presence: true
def price_show
"€ %.2f" % (self[:price]/100.0)
end
def price
self[:price]
end
def self.search(search_term)
Product.where("name LIKE ?", "%#{search_term}%")
end
# Called by <%= #product.highest_rating_comment %>
def highest_rating_comment
comments.rating_desc.first
end
def average_rating
comments.average(:rating).to_f
end
end
and my partial form.
<%= form_with(model: #product, local: true) do |form| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field">
<%= form.label :image_url %>
<%= form.text_field :image_url %>
</div>
<div class="field">
<%= form.label :color %><br>
<%= form.text_field :color %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
The problem I am having is that when I enter in the price, I will be getting the 'undefined error" on both of these lines, to which it will say "did you mean #product?".
div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
I change both the lines accordingly, the page loads. However, when I try to enter the price on the page, I will get an error message, stating that the price 'cannot be blank', even though I am entering a numerical value in the field.
My products controller code is as follows.
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
if params[:q]
search_term = params[:q]
#products = Product.search(search_term)
else
#products = Product.limit(4)
end
end
# GET /products/1
# GET /products/1.json
def show
#comments = #product.comments.order("created_at DESC")
#comments = #comments.paginate(:page => params[:page], :per_page => 2)
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :image_url, :color)
end
end
Am relatively new to rails so sorry if this is a silly question but any help would be greatly appreciated.
price 'cannot be blank'
You should whitelist price in the product_params method to fix the validation error (i.e, price 'cannot be blank') and to able to save the product with its price successfully.
def product_params
params.require(:product).permit(:name, :description, :image_url, :color, :price)
end
Reason for the error:
Since you didn't whitelisted price attribute, it will treated as unpermitted params and won't be included in the params hash while saving the #product. And as save triggers the validations, #product fails the validation check on price resulting in that error.

nested resource only working on edit not new, rails 5.0.0.1

I have the following resources defined:
resources :buildings do
resources :buildings_regular_hours
end
My models are are follows:
class Building < ApplicationRecord
has_many :building_regular_hours
def to_s
name
end
end
class BuildingsRegularHours < ApplicationRecord
belongs_to :building
end
I am attempting to create a form to allow for creating and editing of BuildingRegularHours. Currently the form I have will display on #edit, but will not display on #new.
new.html.erb:
<%= render 'form', buildings_regular_hour: #buildings_regular_hour, building: #building %>
edit.html.erb:
<h1>Editing Buildings Regular Hour</h1>
<%= render 'form', buildings_regular_hour: #buildings_regular_hour, building: #building %>
<%= link_to 'Back', building_buildings_regular_hour_path(#building) %>
_form.html.erb:
<%= form_for([building,buildings_regular_hour]) do |f| %>
<% if buildings_regular_hour.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
<ul>
<% buildings_regular_hour.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :building_id %>
<%= f.text_field :building_id %>
</div>
<div class="field">
<%= f.label :start_date %>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :end_date %>
<%= f.date_select :end_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
buildings_regular_hours_controller.rb:
class BuildingsRegularHoursController < ApplicationController
before_action :set_buildings_regular_hour, only: [:show, :edit, :update, :destroy]
before_action :set_building
# GET /buildings_regular_hours
# GET /buildings_regular_hours.json
def index
#buildings_regular_hours = BuildingsRegularHours.all
end
# GET /buildings_regular_hours/1
# GET /buildings_regular_hours/1.json
def show
end
# GET /buildings_regular_hours/new
def new
#buildings_regular_hour = BuildingsRegularHours.new
end
# GET /buildings_regular_hours/1/edit
def edit
end
# POST /buildings_regular_hours
# POST /buildings_regular_hours.json
def create
#buildings_regular_hour = BuildingsRegularHours.new(buildings_regular_hour_params)
respond_to do |format|
if #buildings_regular_hour.save
format.html { redirect_to #buildings_regular_hour, notice: 'Buildings regular hours was successfully created.' }
format.json { render :show, status: :created, location: #buildings_regular_hour }
else
format.html { render :new }
format.json { render json: #buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /buildings_regular_hours/1
# PATCH/PUT /buildings_regular_hours/1.json
def update
respond_to do |format|
if #buildings_regular_hour.update(buildings_regular_hour_params)
format.html { redirect_to #buildings_regular_hour, notice: 'Buildings regular hours was successfully updated.' }
format.json { render :show, status: :ok, location: #buildings_regular_hour }
else
format.html { render :edit }
format.json { render json: #buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# DELETE /buildings_regular_hours/1
# DELETE /buildings_regular_hours/1.json
def destroy
#buildings_regular_hour.destroy
respond_to do |format|
format.html { redirect_to buildings_regular_hours_index_url, notice: 'Buildings regular hours was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_buildings_regular_hour
#buildings_regular_hour = BuildingsRegularHours.find(params[:id])
end
def set_building
#building = Building.find(params[:building_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def buildings_regular_hour_params
params.require(:buildings_regular_hour).permit(:building_id, :start_date, :end_date, :sunday_id, :monday_id, :tuesday_id, :wednesday_id, :thursday_id, :friday_id, :saturday_id)
end
end
Having added a BuildingRegularHours via console, I attempted the #edit action, and it works just fine, displaying the form as expected. However, when I attempt the #new action, I receive the following error:
Showing /Users/shawn/Documents/uga/library_hours/app/views/buildings_regular_hours/_form.html.erb where line #1 raised:
undefined method `building_buildings_regular_hours_index_path` for #<#<Class:0x007fe9589e2890>:0x007fe95f9bbb30>
Did you mean? building_buildings_regular_hours_path
building_buildings_regular_hour_path
building_buildings_regular_hours_url
building_buildings_regular_hour_url
Extracted source (around line #1):
1 <%= form_for([building,buildings_regular_hour]) do |f| %>
2 <% if buildings_regular_hour.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
5
6 <ul>
Trace of template inclusion: app/views/buildings_regular_hours/new.html.erb
I note that I have properly nested the resources in the form_for tag, that both #building and #building_regular_hour are set by the controller, and that I am calling the form in exactly the same way for both #edit and #new. This is all I've had to do previously to make nested resources work, so I'm at a bit of a loss as what to do next.
Please note I have not attempted to make the form work yet - I know there is work to be done there. I just am trying to get #new to display the form.
You need to correct the association
class Building < ApplicationRecord
has_many :buildings_regular_hours
def to_s
name
end
end
class BuildingsRegularHour < ApplicationRecord
belongs_to :building
end
Your model name should always be singular BuildingsRegularHour or it will create issues with routes and associations

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

HABTM Association Build

I need propagate this values in :departaments_products table: , but I received the error:
I'm using Rails 4
NoMethodError in Products#new
undefined method `departament_id' for #<Product:0x007f916d35d648>
view.html.erb:
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.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>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<%= f.collection_select(:departament_id, Departament.all, :id, :name, {:include_blank => true}) %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Products_controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
#product = Product.find( params[:id] )
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Produto criado com sucesso' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price)
end
end
Models:
class Departament < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :departaments
end
Migration:
class AddProductsAndDepartaments < ActiveRecord::Migration
def change
create_table :departaments_products do |t|
t.references :product, :departament
end
end
end
As its a HABTM association, logically you should be selecting multiple departament_ids for a single product. That said, you should include multiple: true option in the collection_select for departament_ids (Notice departament_ids in plural) in your view code:
<%= f.collection_select(:departament_ids, Departament.all, :id, :name, {include_blank: true}, {multiple: true}) %>
Currently, you are accessing it as departament_id (Notice singular) BUT as per HABTM association you get a method named departament_ids (Notice plural) and NOT departament_id which is why you receive error as NoMethodError in Products#new undefined method 'departament_id'
Once you are done with this change, you need to permit the departament_ids field in ProductsController as below:
def product_params
params.require(:product).permit(:name, :price, :departament_ids => [])
end
:departament_ids => [] is used because multiple selection is allowed for departament_ids and so you would receive it as an Array in params hash upon form submission.
Try departament_ids
For has_many => departament_ids
For has_one => departament_id

acts_as_taggable issue - Everything looks almost fine

I have just followed step by step this other question, but my app is still giving me some errors regarding tagging (rails 4)
Error that Im experiencing is: Desktop/hack/app/controllers/jacks_controller.rb:85: syntax error, unexpected end-of-input, expecting keyword_end end ^
I have already re-intended as suggested in the chat, but nothing changed.
Code for reference
I have no associations between my models, (in the link that I have referred, there are associations)
jack form
<%= form_for(#jack) do |f| %>
<% if #jack.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jack.errors.count, "error") %> prohibited this jack from being saved: </h2>
<ul>
<% #jack.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :tag_list, "Tags (separated by comma)" %><br>
<%= f.text_field :tag_list %>
</div>
<div class="field">
<%= f.label :picture %><br>
<%= f.text_field :picture %>
</div>
<div class="actions">
<%= f.submit %>
jack.rb
class Jack < ActiveRecord::Base
acts_as_taggable_on :tags
end
Routes
Rails.application.routes.draw do
get 'tagged/index'
root :to => redirect('/jacks')
get 'about' => "about#info"
get 'submit' => "jacks#create"
resources :jacks
match 'tagged', to: 'jacks#tagged', :as => 'tagged', via: 'get'
resources :users
jack active helper
module JacksHelper
include ActsAsTaggableOn::TagsHelper
end
And controller
class JacksController < ApplicationController
before_action :set_jack, only: [:show, :edit, :update, :destroy]
# GET /jacks
# GET /jacks.json
def index
if params [:tag]
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.all
end
# GET /jacks/1
# GET /jacks/1.json
def show
end
# GET /jacks/new
def new
#jack = Jack.new
end
# GET /jacks/1/edit
def edit
end
# POST /jacks
# POST /jacks.json
def create
#jack = Jack.new(jack_params)
respond_to do |format|
if #jack.save
format.html { redirect_to #jack, notice: 'Jack was successfully created.' }
format.json { render :show, status: :created, location: #jack }
else
format.html { render :new }
format.json { render json: #jack.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /jacks/1
# PATCH/PUT /jacks/1.json
def update
respond_to do |format|
if #jack.update(jack_params)
format.html { redirect_to #jack, notice: 'Jack was successfully updated.' }
format.json { render :show, status: :ok, location: #jack }
else
format.html { render :edit }
format.json { render json: #jack.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jacks/1
# DELETE /jacks/1.json
def destroy
#jack.destroy
respond_to do |format|
format.html { redirect_to jacks_url, notice: 'Jack was successfully destroyed.' }
format.json { head :no_content }
end
end
def tagged
if params[:tag].present?
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.postall
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_jack
#jack = Jack.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def jack_params
params.require(:jack).permit(:title, :description, :picture, :tag_list)
end
end
index method is missing an 'end'
def index
if params[:tag]
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.all
end
end

Resources