Submit into two tables with single form submit - ruby-on-rails

Is There Any way we can submit into two tables in single rails form_for/simple_form_for which both the tables are associated to each other?
Example:-
<%= form_for #ob do |f| %>
<%= f.text_field :name %>
<%= f.text_field :another_col --> this is another table column(having association)
<%f.button :submit %>
<%end%>

The trick is to add accepts_nested_attributes_for to your parent model, and do as below
MODEL
class Ob < ActiveRecord::Base
has_many :foo
accepts_nested_attributes_for :foo, allow_destroy: true
end
ERB
<%= form_for #ob do |f| %>
<%= f.text_field :name %>
<% f.foo do |fo| %>
<%= fo.text_field :another_col %> <%=# this is another table column(having association)%>
<% end %>
<%f.button :submit %>
<%end%>
CONTROLLER
#your params would look like below
params = { ob: {
name: 'joe', foos_attributes: [
{ another_col: 'Bar' }
]
}}
Ob.create(params[:ob])

Related

How to have multiple forms for one model in one view

I need to set the Assignments on a Game by having all assignments associated to a game in one form. When I go to the edit_assignment page though there's only 1 input. If I have 3 assignments, for example, on one game. How do I structure the form to display the 3 inputs AKA the 3 assignments? It's probably also worth noting that I'd want to render as many inputs as there are assignments created for each game.
I tried something along the lines of this to no avail:
<%= #game.assignment do |a| %>
<div>
<%= simple_form_for(a) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.input :user_id, label: "C: " do %>
<%= f.select :user_id, User.all.map { |r| [r.first_name, r.id] }, {include_blank: "Select Referee" } %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Models:
class Game < ApplicationRecord
has_many :assignments
has_many :users, through: :assignments
end
class Assignment < ApplicationRecord
belongs_to :game
belongs_to :user
end
What you are doing is rendering the form x times for each assignment if a game has x assignments. Instead of rendering form you need x input fields, so instead of looping form, loop the input fields for each assignment with different assignment id, something like this:
<div>
<%= form_with #game do |f| %>
<%= form.fields_for :assignments do |assignment| %>
<%= assignment.input :user_id, label: "C: " do %>
<%= assignment.select :user_id, User.all.map { |r| [r.first_name, r.id] }, {include_blank: "Select Referee" } %>
<% end %>
<% end %>
</div>

edit associated model by input form?

Order has_many jobs
Job belongs to order
And I want to edit attributes of #job.order:
<% order = #job.order %>
<%= simple_form_for [#job, order],
url: job_path(#job),
method: :put,
remote: true do |f| %>
<%= f.input :order_status, input_html: {class: 'form-control'} %>
(...)
<% end %>
any way to do it by just using input in simple form?
in job.rb
accepts_nested_attributes_for :order
in form.html.erb
simple_form_for #job do |f|
f.simple_fields_for #job.order do |order_form|
order_form.input :status
end
end
in jobs_controller.rb
params.require(:job).permit(:something, :something_else, :order_attributes => [:status])
You can use the excellent Cocoon gem https://github.com/nathanvda/cocoon to manage nested relationships, including the ability to easily add new nested relationships.
class Job < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders, reject_if: :all_blank, allow_destroy: true
end
class Order < ActiveRecord::Base
belongs_to :job
end
Note the pluralization.
_form.html.erb*
<%= form_for #job do |f| %>
<%= f.label :job_name %>
<%= f.text_field :name %>
<div id='order'>
<%= f.fields_for :orders do |order| %>
<%= render 'order_fields', f: order %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add order', f, :orders %>
</div>
<%= f.submit %>
<% end %>
_order_fields.html.erb partial
<div class='nested-fields'>
<%= f.label :order_name %>
<%= f.text_field :order_name %>
</div>
<%= link_to_remove_association "remove order", f %>

HABTM association dropdown select

I am trying to create a dropdown select on a form. I have a HABTM association between professors and classrooms:
Classroom Model:
class Classroom < ApplicationRecord
has_and_belongs_to_many :professors
end
Professor Model:
class Professor < ApplicationRecord
has_and_belongs_to_many :classrooms
end
Strong Params:
def classroom_params
params.require(:classroom).permit(:name, :professor_ids => [])
end
I am trying to find a way to use f.select instead of select_tag inside the form. But when I do it, the database does not save the values. This way works:
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<% array = Professor.all.map { |professor| [professor.user.name, professor.id] } %>
<%= select_tag "classroom[professor_ids][]", options_for_select(array) %>
<% end %>
But I am trying like that and it is not working:
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<% array = Professor.all.map { |professor| [professor.user.name, professor.id] } %>
<%= f.select :professor_ids, options_for_select(array) %>
<% end %>
The view works correctly but when I submit the form, the value doesn't go to to the classroom_params. I tried to debug it stopping the controller after the submit and I got this:
The params came correctly with all the information submitted, but the classroom_params came missing the professor_ids.
Is there a way to do this dropdown using f.select?
You whitelisted the array of 'professor_ids', but your 'select' input returns 1 string ("prefessor_ids" => "2" from you screenshot). Maybe you want to set the select as 'multiple'? (I have not tested it, but i think params will be whitelisted correctly after that)
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<%= s.collection_select :professor_ids, Professor.all, :id, :name, multiple: true %>
<% end %>
where
class Professor
...
delegate :name, to: :user
end
Update
You probably don't have 'cfg' variable in your controller action.

Rails Nested Resources - Many Objects, Same Model, One Form Without Existing Objects

I have a model Vendors which has many Products. I would like to add many Products at a time without showing the existing products that belong to the vendor.product relationship. I only want to display the form for new objects. Currently everything is working but on the add page I am getting all of the objects that are tied to the instance relationship which is #vendor.products. If I do not use that relationship in the form I do not get any fields.
Here is my new Product action:
'def new
#vendor = Vendor.find(params[:vendor_id])
5.times {#vendor.products.build}
end'
Here is my form:
<%= form_for #vendor do |f| %>
<%= f.fields_for :products do |g| %>
<p>
<%= g.label :name %>
<%= g.text_field :name %>
<%= g.label :category %>
<%= g.select :category, options_for_select(['Parts', 'Labor', 'Extras', 'Shop']) %><br>
</p>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
The Product Model:
class Product < ActiveRecord::Base
belongs_to :vendors
attr_accessible :name, :category, :vendor_id, :vendor_sku, :products
validates :name, :uniqueness => true
validates :category, :presence => true
validates :name, :presence =>true
end
Just to reiterate, I would only like to show the blank, newly built objects as opposed to all of the products tied to that #vendor 's relationship. I must be overlooking a form structure to get this done but I just have not been able to figure it out. Thanks for looking.
Only render the nested fields if the product in a new record.
<%= form_for #vendor do |f| %>
<%= f.fields_for :products do |g| %>
<% if g.object.new_record? %>
<p>
<%= g.label :name %>
<%= g.text_field :name %>
<%= g.label :category %>
<%= g.select :category, options_for_select(['Parts', 'Labor', 'Extras', 'Shop']) %><br>
</p>
<% end %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>

rails how to pass nested parameters

I have a strong parameter like this
params.require(:survey).permit( option_booleans_attributes: [:id, :survey_id, :topic, :answer])
if I use rails f.input I got parameter like
"option_booleans_attributes"=>{"0"=>{"answer"=>"true", "id"=>"5"}, "1"=>{"answer"=>"false", "id"=>"6"}, "2"=>{"answer"=>"true", "id"=>"7"}}}
but I need to show option_booleans topic and let user fill answer
<% #survey.option_booleans.each do |question| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "true" %>是
<%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "false" %>否<br>
<% end %>
But I don't know how to generate the 0 1 2 in the parameter..
about my survey.rb
class Survey < ActiveRecord::Base
has_many :option_booleans
accepts_nested_attributes_for :option_booleans
belongs_to :member
end
class OptionBoolean < ActiveRecord::Base
belongs_to :survey03
and OptionBoolean have topic:string and answer:boolean
I want to let user see the topic and update the answer
It seems you're missing several parts of your form:
#app/controllers/surveys_controller.rb
def new
#survey = Survey.new
#survey.option_booleans.build #-> creates ActiveRecord Object for your form
end
#app/views/surveys/new.html.erb
<%= form_for #survey do |f| %>
<%= f.fields_for :option_booleans do |option| %>
<% #survey.question_booleans.each do |question| %>
<%= question.topic %>
<%= f.radio_button "answer[#{question.id}]", "true" %>
<%= f.radio_button "answer[#{question.id}]", "false" %>
<% end %>
<% end %>
<% end %>
Although I don't quite understand how your system works
You have a survey with many options, but you're passing new attributes to create new options each time. Surely you'd have option_booleans as a join model, with options as an independent model, and:
#app/models/survery.rb
Class Survey < ActiveRecord::Base
has_many :option_booleans
has_many :options, through: :option_booleans
end
What I use now
<li><% #survey03.option_booleans.each do |question| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "true", :required => true %>
<%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "false", :required => true %>
<% end %></li>
and in controller
params[:option_booleans_attributes].each do |option_id, attributes|
if option = OptionBoolean.find_by_id(option_id)
option.update_attributes(attributes)
end
end
and I know if I want to generate index just use
<% #survey.option_booleans.each_with_index do |question, index| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "true" %>
<%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "false" %>
<% end %>
try select options like this:
#variable_with_booleans = [true, false]
<%= f.select :param, #variable_with_booleans %>
answer to your latest comment:
I don't sure, but try to edit your form
<%= form_for #question do |f| %>
<%= f.label :topic %>
<% #variable_with_booleans = [true, false] %>
<%= f.select :answer, #variable_with_booleans %>
<%= f.submit %>
<% end %>
and edit your strong params in controller,which contains this action

Resources