Nested_form_for with field_for not working - ruby-on-rails

my view is
<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for #item_category, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
<div><%= f.label :item_name %>
<%= f.text_field :item_name %></div>
</br>
<div>
<%= f.fields_for :item_companies do |f| %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
</div>
</br>
<%= f.fields_for :item_weights do |f| %>
<div>
<%= f.label :weight %>
<%= f.text_field :weight %>
</div>
</br>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<%end%>
<%end%>
<div><%= f.submit "Submit" %></div>
<% end %>
</div>
and controller
def new
#item_category = ItemCategory.new
item_company = #item_category.item_companies.build
item_company.item_weights.build
end
and when I have applied debugger in create action and I saw value of params[:item_category] and this is generated {"item_name"=>"iname", "item_companies_attributes"=>{"0"=>{"company_name"=>"cname", "item_weights_attributes"=>{"0"=>{"weight"=>"20kg", "price"=>"100"}}}}}
ItemCategory.new(params[:item_category])
generated error
ActiveModel::MassAssignmentSecurity::Error Exception: Can't mass-assign protected attributes: item_companies_attributes.
Where i am wrong and how to save value in three tables using this.

It seems that you forgot to add this piece of code in your ItemCategory model:
attr_accessible :item_companies_attributes
which will allow you to set these attributes.

Related

How to get the value of previous select option in form_for rails?

<%= form_for #bug do |f| %>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="form-group">
<%= f.label :Bug_Type %>
<%= f.select :bugtype, [["Feature","Feature"], ["Bug","Bug"]], required: true %>
</div>
<div class="form-group">
<% if f.object.bugtype == 'Feature' %>
<%= f.label :Bug_Status%>
<%= f.select :status,
[
["New","New"],
["Started","Started"],
["Resolved","Resolved"]
],
required: true %>
<% else %>
<%= f.label :Bug_Status %>
<%= f.select :status,
[
["New","New"],
["Started","Started"],
["Completed","Completed"]
],
required: true %>
<% end %>
</div>
<%= f.submit %>
<% end %>
I have this code in my form. I want to know the value user selects for :bugtype, in order to display status accordingly. I am not able to access value of bugtype using instance variable or f.object.bugtype

search for select in rails

I have a rails app where user can assign task to another user by creating task action. At the moment I have the f.collection_select, but as the number of users grows it won't be a good solution. I would like to change it into kinda search form. My problem is that I don't see how I can do this. I have other search forms, but those are for show/index action. How can I do this for the create action?
here is my current code:
<%= form_for ([#user, #task]) do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
<div class="field form-group">
<%= f.label :executor_id %>
<%= f.collection_select(:executor_id, User.all, :id, :email, class: "form-control") %>
</div>
<div class="field form-group">
<%= f.label :content %>
<%= f.text_area :content, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :deadline %>
<%= f.date_select :deadline, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %>
</div>
<% end %>

Pass a value to a Form in rails

I have to pass a value in a form where the user cannot to choose. I have a Register model with some fields. One of them is a value from another Model called Car. I show in the _form view the car plate value but I want to link this value in the form This is the code in _form :
<%= form_for(#reg) do |f| %>
<div class="field">
<p><%= f.label :date %></p><br>
<%= f.date_field :date_reg %>
</div>
<div class="field">
<p><%= f.label :car_id %></p><br>
<% Car.all.each do |car| %>
<%= car.plate %>
<%= f.select(:driver_id,
options_from_collection_for_select(Driver.all, :id,
:name), {:prompt => 'Please Choose'}, :class => "form-
control") %><br>
<% end %>
</div>
<div class="field">
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Thanks all

If statement preventing submit button to work

My submit button won't work because of the first if statement. If I remove the block, the button works. The statement seems to break the view. Why?
<% if params[:action] == "edit" %>
<div class="field">
<%= form_tag :action => "edit" do %>
<%= select_tag :vehicle_id, options_from_collection_for_select(#vehicles, :id, :model, params[:id].to_i), :onchange => "this.form.submit()" %>
<% end -%>
<%= link_to 'Nouvelle voiture', new_vehicle_path %>
</div>
<% end -%>
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<% if params[:action] == "edit" %>
<%= link_to 'Détruire', #vehicle, method: :delete, data: { confirm: 'Êtes-vous sûr ? Les trajets associés seront aussi détruits.' } %>
<h1>Trajets</h1>
<div>
<span>Clients</span>
<span>Kms aller/retour</span>
</div>
<%= f.fields_for :trip_distances do |builder| %>
<div class="field">
<%= builder.text_field :id_contract %>
<%= builder.number_field :length %>
</div>
<% end -%>
<% end -%>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
When you put if statement you don't have form in which your submit button resides. Without if statement your form is in the view, and you can submit to it.
Found the solution. I didn't need the form_tag:
<%= form_tag :action => "edit" do %>

How to ensure all nested attributes show during 'edit'

I currently have a nested model User has_many Sales_Orders has_many Items.
I can create the Sales_Order with nested Items properly but when I try to 'edit' the Sales_Order, the Sales_Order information is visible but the Items are not shown. Any idea why these wouldn't show?
[sales_orders_controller.rb]
...
def edit
#sales_order = SalesOrder.find(params[:id])
end
[edit.html.erb]
<% provide(:title, "Edit SO") %>
<h1>Edit Sales Order</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#sales_order) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="span3">
<%= f.label :so, "SO#:" %>
<%= f.text_field :so %>
<%= f.label :customer, "Customer:" %>
<%= f.text_field :customer %>
<%= f.label :enter_date, "Date Entered:" %>
<%= f.text_field :enter_date, value: date_formatter(#sales_order.enter_date) %>
<%= f.label :request_date, "Request Date:"%>
<%= f.text_field :request_date, value: date_formatter(#sales_order.request_date) %>
<%= f.label :comments, "CS Comments:" %>
<%= f.text_area :comments %>
</div>
<div class="span3">
<% f.fields_for #sales_order.items do |builder| %>
<%= render 'item_fields', f: builder %>
<% end %>
</div>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
[_item_fields.erb]
<fieldset>
<%= f.label :item_code, "Item Code:" %>
<%= f.text_field :item_code %>
<%= f.label :qty_in_kg, "Qty (kg):" %>
<%= f.text_field :qty_in_kg %>
<%= f.label :qc_comments, "Comments:" %>
<%= f.text_field :qc_comments %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
<% f.fields_for #sales_order.items do |builder| %>
should be
<%= f.fields_for #sales_order.items do |builder| %>
Without the = you're building the output in the loop but never rendering/printing the return value (the generated HTML output).
Use <%= f.fields_for #sales_order.items do |builder| %>
Notice the = sign. This implies, "evaluate and embed"

Resources