I have a model Driver which has many relationship to claims, I wish to have a form that client can enter their detail, together with a number of claims they've had in the past.
In the model, I have:
has_many :claims
and in the form I entered:
<%= semantic_form_for #driver do |f| %>
<%= f.input :name %>
.......
<%= f.inputs :for => :claims do |c| %>
<% c.input :happen_date %>
<% c.input :claim_details %>
...........
<% end %>
<% end %>
When I submit the form, the issue happens, I got Claim(#3213231311) expected, got Array(#70299228017580)
And I had a look at the form, I saw:
{"utf8"=>"✓",
"authenticity_token"=>"VD3lt+LXZoA94YoL3PkI0frTH5EzT4vs/lZhzQhp0IQ=",
"driver"=>{
"name"=> "test tester",
.......
"claims"=>{"happend_date(3i)"=>"10",
"claim_date(2i)"=>"10",
"claim_date(1i)"=>"2012",
"claim_details"=>"dsadsadsadas"}},
"commit"=>"Next"}
Since there is a has_many relationship with the claims, shouldn't claims be covered in an array like:
"claims" => [{.....},{......}]
I've been working so hard on the form and still couldn't get claims to be send as array, anyone help?
Did you include this in your 'Driver' model:-
accepts_nested_attributes_for :claims
Edit your form like :
<%= semantic_form_for #driver do |f| %>
<%= f.input :name %>
<%= f.semantic_fields_for :claims do |c| %>
<% c.input :happen_date %>
<% c.input :claim_details %>
<% end %>
<% end %>
Hope it will help. Thanks
Related
I have an Author and Book model. An Author can have many Book, but a Book can only have one Author.
During the creation of a new Author, how do I add a "New Book" button which will add a fields_for for the Author form? I want to be able to add more than one Book. So far, the solution that I have found is just to add a fixed number of fields_for, like this:
<%= form_with(model: author, local: true) do |form| %>
...
<% 5.times do %>
<%= form.fields_for :books do |books| %>
...
<% end %>
<% end %>
...
<% end %>
I do not want it to be like this, what if the author has more than 5 books? I want to be able to add books dynamically. Any solutions?
If you fine with adding a new dependency, there's cacoon gem, which allows you easier handle nested forms.
It's an additional dependency, but the code for adding a new associated record is simple as:
<%= form_for author do |f| %>
...
<%= f.fields_for :books do |book| %>
<%= render 'book_fields', f: book %>
<%= link_to_add_association 'add book', f, :books %>
<%end%>
author.rb
accepts_nested_attributes_for :books,
:allow_destroy => true,
:reject_if => :all_blank
In your view file
<div class="bank_account_details">
<%= f.fields_for :bank_account_details do |builder| %>
<p>
Book: <%= builder.text_field :name %>
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Book" %>
</p>
<% end %>
<div>
<%= link_to_add_fields "Add Book", f ,:books %>
In your controller add parameters
params.require(:author).permit(:name, books_attributes: [:id, :name, :_destroy])
I have a form that saves attributes to two models Company and nested model Address in a single form using simple_nested_form_for and simple_fields_for.
The attributes:
Company:
-legal form
-uploads
Address:
-name
-website
In my form I wish to change the order of the fields
-name (Address model)
-legal form (Company model)
-website (Address model)
-uploads (Company model)
thus interchanging the form attributes and nested_field attributes
I tried the following, but that did not seem to work.
<%= simple_nested_form_for #company do |f| %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :name %>
<% end %>
<%= f.input :legal_form %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :website %>
<% end %>
<%= f.input :uploads %>
<% end %>
How do I make this work?
While it doesn't look like all your attributes line up ( you said uploads was address and website was in company in your attributes list but the form doesn't match that ) there is still a simple solution to your question.
Nested fields rely on their builder to denote which part of the params hash to put them in. So simply call the builder that you need.
<%= simple_nested_form_for #company do |f| %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :name %>
<%= f.input :legal_form %>
<%= c.input :website %>
<%= f.input :uploads %>
<% end %>
<%= f.submit %>
<% end %>
In each place that you need the company fields, you'll call builder f and in each place you need the address fields, you'll call builder c - but nested them all inside the lowest common denominator so they're all available.
I have a new action in the marks controller. When the user clicks on 'new marks', I need to display a form which contains list of papers for that student. And each paper has a list of options which the user needs to select.
The association between the models are:
Mark.rb
belongs_to :paper
paper.rb
has_many :options
Option.rb
belongs_to :paper
In the form for #mark, I need to display all the papers and the list of options using 'formtastic'.
I tried doing,
<% #array_papers.each do |paper| %>
<% options = paper.options %>
<%= semantic_form_for paper, url:thinking_marks_path(student_id: #student.id) do |form| %>
<li class="each-question">
<%= form.input :paper, label: "{paper[:name]}" %>
<%= semantic_fields_for :options, paper.object.options do |option| %>
<%= option.input :option, as: :check_boxes %>
<% end %>
</li>
<% end %>
</ul>
<p> <%= link_to 'Save',thinking_marks_path( student_id: #student.id ), :class => 'simple-button course-type' %>
</p>
<% end %>
But it is throwing error:
undefined method `option' for #<Paper:0x0000000fe6d3e0>
What should I do ?
The form variable is the form of paper object.
I advise you to better naming your form variables, eg. paper_form and option_form.
This code should work:
<%= semantic_fields_for :options, paper.object.options do |option_form| %>
<%= option_form.input :option, as: :check_boxes %>
<% end %>
Very new to Rails. I'm trying to create a form using Formtastic in which a user creates many Picks in a single form. Each Pick belongs to a Game and each Game belongs to a Week. I'm reasonably certain I've correctly declared these relationships in my models.
The problem: I'd like to create Picks for all of a Week's Games and properly save each child Pick with an index that allows me to associate it with the correct parent Game.
Here is the new.html.erb code for Weeks that makes no attempt to index the Picks.
<%= semantic_form_for #week do |f|%>
<%= f.inputs do %>
<% week_slate.each do |g| %>
<%= f.semantic_fields_for :game do |game| %>
<%= game.inputs do %>
<%= game.semantic_fields_for :pick do |pick| %>
<%= pick.inputs %>
<%= pick.input :winner, :as => :radio, :label => "", :collection => [[g.visiting_team, false], [g.home_team, false]] %>
<%= pick.input :wager %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
Edit: week_slate is an array that contains all of the Games that belong to the current Week.
Please advise & many thanks!
I'm having issues with the implementation of a nested form for a model with has_many through relationship
I have 3 models: Reservation, Table and Collection (Join Model for these two)
In my Reservation Controller I have these two methods:
def new
#reservation = Reservation.new
#tables = Tables.all
#tables.size.times {#reservation.collections.build}
end
and
def reservation_params
params.require(:reservation).permit(:name, collections_attributes: [ :table_id, :units_sold],
tables_attributes: [:units, :title])
end
my form view is as following:
<%= form_for [current_user, #reservation] do |f| %>
<header>
<h1>Make Reservation</h1>
</header>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.fields_for :collections do |builder| %>
<%= render 'nested_form', :f => builder %>
<% end %>
<%= f.submit "Save" %>
<% end %>
and my _nested_form.html.erb file:
<%= f.number_field :units_sold, placeholder: "Units" %>
<%= check_box_tag :table_id %>
My problem is, whenever I save a new entry on the database he assigns the same table_id to all collections association, e.g:
I want to receive the Parameters hash such as:
"collections_attributes"=>{"0"=>{"units_sold"=>"5", "table_id"=>"1"}, "1"=>{"units_sold"=>"6", "table_id"=>"2"}}}
Instead what I'm getting is:
"collections_attributes"=>{"0"=>{"units_sold"=>"5", "table_id"=>"1"}, "1"=>{"units_sold"=>"6", "table_id"=>"1"}}}
How do I fix this for it to give the correct table_id for each collection?
It would be better if you provide your models' snippet that shows their associations.
Anyway, according to your form; One Reservation has many tables through collections.
change:
<%= check_box_tag :table_id %>
to
<% #tables.each do |table| %>
<li>
<%= f.radio_button :table_id %>
<%= f.label :table_id, table.name %>
</li>
<% end %>
By this, you will be able to choose one table from all for each collection using radio button.
Hope this will help!