i made a nested form for my invoice application with the cocoon gem but the form isn't showing on my application but it isn't giving out any errors either.
_form.html.erb - scaffold form partial
<address>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
_customer_fields.html.erb - cocoon partial
<div class="nested-fields">
<div class="form-group">
<%= f.label 'Company Name' %><br/>
<%= f.text_field :company_name, placeholder: 'company name' %>
</div>
<div class="form-group">
<%= f.label 'Address' %><br>
<%= f.text_field :address_line_1, placeholder: 'address' %>
</div>
<div class="form-group">
<%= f.label 'Zip Code' %><br>
<%= f.text_field :zip_code %>
</div>
<%= link_to_remove_association "remove customer", f, class: 'btn btn-primary' %>
</div>
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
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
customer.rb model
class Customer < ActiveRecord::Base
belongs_to :invoice
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
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])
end
end
Does anybody know how to fix this? Any help would be much much appreciated!
try it
<address>
<%= f.object.build_customer if f.object.customer.nil? %>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
edit
in the Luissimo solution was missing to insert invoice_id in customer schema
rails generate migration AddInvoiceIdToCustomer invoice_id:integer
rake db:migrate
Invoice.first.build_customer
Looking at your original code, in your controller you should have added:
def new
#invoice = Invoice.new
#invoice.customer.build
end
Related
It was working perfectly fine but now I'm getting this error for some reason?
class Post < ApplicationRecord
belongs_to :user
geocoded_by :address
after_validation :geocode, if: :address_changed?
has_many :rsvps
has_many :users, through: :rsvps
end
form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="action">
<%= form.label :address %>
<%= form.text_area :address, id: :post_address %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
post controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == #post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
end
migrations
class AddLatitudeAndLongitudeToPost < ActiveRecord::Migration[5.1]
def change
add_column :posts, :latitude, :float
add_column :posts, :longitude, :float
add_column :posts, :address, :string
end
end
posts migration
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.datetime :date
t.string :name
t.integer :user_id
t.text :description
t.timestamps
end
end
end
I'm not sure how it would stop working automatically as there was literally no changes. I was just figuring out why my google static map was getting an error. However I didn't implement any changes so I don't know what could have possibly caused this error? I would appreciate some advice, thank you.
The possible reason for ActiveModel::UnknownAttributeError is when you try to assign an attribute which is not present in the table.
You must have added some attributes to Post model but must have forgotten to run the migration
bundle exec rake db:migrate
Running this command should solve the problem
NOTE: I can see there is a new migration AddLatitudeAndLongitudeToPost which is adding some columns to posts table. Check the migration status with:
bundle exec rake db:migrate:status
Check if you can see same attributes in schema.rb
I'm trying to create a nested form in Ruby on Rails. The form appears as expected. But when it is saved the nested attribute, Booking, is not saved.
cleaner.rb
class Cleaner < ActiveRecord::Base
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :quality_score
validates :quality_score, inclusion: 0.0...5.0
has_many :assignments
has_many :bookings
has_many :cities, through: :assignments
has_many :customers, through: :bookings
end
customer.rb
class Customer < ActiveRecord::Base
validates_presence_of :first_name
validates_presence_of :last_name
validates_uniqueness_of :phone_number
belongs_to :city
has_many :bookings
has_many :cleaners, through: :bookings
accepts_nested_attributes_for :bookings
end
booking.rb
class Booking < ActiveRecord::Base
belongs_to :customer
belongs_to :cleaner
validates_presence_of :customer
validates_presence_of :cleaner
validates_presence_of :date
end
customers_controller.rb
class CustomersController < ApplicationController
before_action :set_customer, only: %i[show edit update destroy]
def index
#customers = Customer.all
end
def show; end
def new
#customer = Customer.new
end
def edit; end
def create
#customer = Customer.find_or_initialize_by(phone_number: params[:phone_number])
#customer.assign_attributes(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
def destroy
#customer.destroy
respond_to do |format|
format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_customer
#customer = Customer.find(params[:id])
end
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone_number, :city, :city_id, bookings_attributes: %i[cleaner_id date])
end
end
HomeController.rb
class HomeController < ApplicationController
def index
#customer = Customer.new
#customer.bookings.build
end
end
Parameters
{"utf8"=>"✓", "authenticity_token"=>"c4xo2M4r57+/xBsmcc+7yajpQU13u1kiwmOthx/nP7HiJXJIfS9/OqC0MrWCcaDrSW/xN8UGk2+LVfnUnbTb3A==", "customer"=>{"first_name"=>"adfad", "last_name"=>"fad", "phone_number"=>"9392323", "city_id"=>"1", "bookings_attributes"=>{"0"=>{"cleaner_id"=>"1"}}}, "#<ActionView::Helpers::FormBuilder:0x007f86bec96480>"=>{"date(1i)"=>"2017", "date(2i)"=>"8", "date(3i)"=>"2"}, "commit"=>"Create Customer"}
updated form
<h1>Sign Up Now</h1>
<%= form_for #customer do |f| %>
<% if #customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% #customer.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 :phone_number %><br>
<%= f.text_field :phone_number %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.select :city_id, options_for_select(City.pluck(:name, :id)) %>
</div>
<%= f.fields_for :bookings do |b| %>
<%= b.date_select :date %>
<br>
<%= b.select :cleaner_id, Cleaner.all.pluck(:first_name, :id) %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
It's now failing with 2 errors
Bookings customer can't be blank
Bookings date can't be blank
use the iteration of the first #customer so rails can read properly
i saw this example https://www.sitepoint.com/complex-rails-forms-with-nested-attributes/
<%= f.fields_for :booking do |ff| %>
<%= ff.select :city_id,options_for_select(City.pluck(:name, :id)) %>
i hope it works cross finger :)
I'm trying to link movies with a specific genre via a form in rails. I have seeded several genre instances in the db and have created a select tag that should, in theory, give a movie a genre_id based on the genres seeded.
Here is my Movie Migration:
class CreateMovies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :name
t.integer :year
t.string :director
t.string :lead_actor
t.references :genre, index: true, foreign_key: true
t.timestamps null: false
end
end
end
Here is the form:
<%= form_for(#movie) do |f| %>
<% if #movie.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#movie.errors.count, "error") %> prohibited this movie from being saved:</h2>
<ul>
<% #movie.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :genre_id %><br>
<%= f.select(:genre_id, #genres.map {|p| [ p.name, p.id ] }) %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.number_field :year %>
</div>
<div class="field">
<%= f.label :director %><br>
<%= f.text_field :director %>
</div>
<div class="field">
<%= f.label :lead_actor %><br>
<%= f.text_field :lead_actor %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
All of my dropdown boxes are being displayed properly, but when I go into the database and check the movies genre_id, it equals 'nil'.
Full controller:
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
# GET /movies
# GET /movies.json
def index
#movies = Movie.all
end
# GET /movies/1
# GET /movies/1.json
def show
end
# GET /movies/new
def new
#movie = Movie.new
#genres = Genre.all
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
def create
#movie = Movie.new(movie_params)
respond_to do |format|
if #movie.save
format.html { redirect_to #movie, notice: 'Movie was successfully created.' }
format.json { render :show, status: :created, location: #movie }
else
format.html { render :new }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
def update
respond_to do |format|
if #movie.update(movie_params)
format.html { redirect_to #movie, notice: 'Movie was successfully updated.' }
format.json { render :show, status: :ok, location: #movie }
else
format.html { render :edit }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# DELETE /movies/1
# DELETE /movies/1.json
def destroy
#movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
#movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:name, :year, :director, :lead_actor)
end
end
I'm having problem with some attributes which are not a part of my model. I'm trying to design a simple payment page as you can see below, all the fiels are represented in my database, except card_number and card_verification for security reasons.
When I load the page it is throwing an error:
undefined method `card_number' for #<Order:0x00000004eddb00>
undefined method `card_verification' for #<Order:0x00000004eddb00>
What's wrong with my form?
Thanks.
The Form:
<%= simple_form_for(#order, html:{class: "well"}) do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :card_type, collection: ["Visa", "MasterCard"] %>
<%= f.input :card_expires_on %>
<%= f.input :card_number %>
<%= f.input :card_verification %>
<%= f.button :submit %>
<% end %>
order.rb:
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :participation
end
orders_controller.rb:
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
def new
#order = Order.new
end
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
private
def set_order
#order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:ip_address, :first_name, :last_name, :card_type, :card_expires_on, :user_id, :participation_id)
end
end
I think the part that is throwing the error should be this in your form:
<%= f.input :card_number %>
<%= f.input :card_verification %>
You could try adding:
attr_accessor :card_number, :card_verification
to your model.
I understand some railscasts may be old, but revised shouldn't be. I am trying really hard to overcome nested form, but most answer i get is use a plugin. So i try to make the railcasts from scratch but an exception occurs. I am wondering what would be the Railcasts 196 Updated Version has today with proper code.
Here my code, maybe its a silly mistake from me.
Survey Model
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :name, :questions_attributes
end
Question Model
class Question < ActiveRecord::Base
belongs_to :survey
attr_accessible :content, :question_id
accepts_nested_attributes_for :answers , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :name, :answers_attributes
end
Answer Model
class Answer < ActiveRecord::Base
belongs_to :questions
attr_accessible :content, :question_id
end
Survey Show Form
<p id="notice"><%= notice %></p>
<div>Name:</div>
<div><%=#survey.name%></div>
<% for question in #survey.questions %>
<div><%=h question.content%></div>
<% end %>
<%= link_to 'Edit', edit_survey_path(#survey) %> |
<%= link_to 'Back', surveys_path %>
Form.html.erb
<%= form_for(#survey) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |bf|%>
<% render 'question_fields, :f => bf %>
<% end %>
***<div class="actions">***
<%= f.submit %>
</div>
<% end %>
question_field Form
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows=> 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question"%><br />
<%= f.fields_for :answer do |form| %>
<%= render 'answer_fields', :f => form %>
<% end %>
answer_field Form
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
Survey Controller
class SurveysController < ApplicationController
# GET /surveys
# GET /surveys.json
def index
#surveys = Survey.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #surveys }
end
end
# GET /surveys/1
# GET /surveys/1.json
def show
#survey = Survey.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #survey }
end
end
# GET /surveys/new
# GET /surveys/new.json
def new
#survey = Survey.new
3.times {#survey.questions.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #survey }
end
end
# GET /surveys/1/edit
def edit
#survey = Survey.find(params[:id])
end
# POST /surveys
# POST /surveys.json
def create
#survey = Survey.new(params[:survey])
respond_to do |format|
if #survey.save
format.html { redirect_to #survey, notice: 'Survey was successfully created.' }
format.json { render json: #survey, status: :created, location: #survey }
else
format.html { render action: "new" }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# PUT /surveys/1
# PUT /surveys/1.json
def update
#survey = Survey.find(params[:id])
respond_to do |format|
if #survey.update_attributes(params[:survey])
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
#survey = Survey.find(params[:id])
#survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url }
format.json { head :no_content }
end
end
end
Here my error when creating a new survey
Not sure what else could be wrong
ArgumentError in SurveysController#create
No association found for name `answers'. Has it been defined yet?
Here the new error
SyntaxError in Surveys#new
Showing /home/jean/rail/surveysays/app/views/surveys/_form.html.erb where line #22 raised:
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:22: syntax error, unexpected keyword_class, expecting keyword_do or '{' or '('
#output_buffer.safe_concat(' <div class="actions">
^
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: unterminated regexp meets end of file
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: syntax error, unexpected $end, expecting keyword_end
Add has_many :answers to the Question model and change the Answer model to belongs_to :question.