Hidden value not being passed in rails app (Head first rails book) - ruby-on-rails

I am following the Head first Rails book for creating an airline ticketing system although I am using version 3.2.13 of rails.
I have a hidden field for flight_id in a partial for adding new seats to a flight. This partial is then rendered on the flights show page. But when I view the outputted html the value for flight_id I get this,
<input type="hidden" value="flight_id" name="seat[flight_id]" id="seat_flight_id">
My code in the partial is
<%= form_for(seat) do |f| %>
<%= f.hidden_field :flight_id, value: :flight_id %>
<div class="field">
<%= f.label :baggage %><br />
<%= f.text_field :baggage %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In my models I have
Flight.rb
has_many :seats
Seats.rb
belongs_to :flight
How do I pass the flight Id from the flight_id to the hidden field?

The flight_id is not available directly.
You can change this line
<%= f.hidden_field :flight_id, value: :flight_id %>
To
<%= f.hidden_field :flight_id, value: seat.flight.id %>

Related

Displaying an objects properties different from default behavior in form helper rails

How can I go about displaying the :cords property below as a formatted array itself? and not just it's values. Given the form below:
<%= form_for #group do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :cords %><br>
<%= f.text_field :cords, name: "group[cords]" %>
</p>
<p>
<%= f.label :members %><br>
<%= f.text_field :members %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
When editing the object, I'd get something like
But what I want to see is the full raw data, like so:
I think this can be done with to_s, but i'm not sure how to go about getting this behavior
You could try using an input tag instead text_field helper:
<input type="text" name="group[cords]" value="<%= group.cords %>">

Rails bootstrap_form_for: text_area is automatically hidden

I made a very simple form using bootstrap_form_for:
<%= bootstrap_form_for(#ad) do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body %>
<%= f.text_field :image %>
<%= f.text_field :user_id %>
<%= f.submit %>
<% end %>
However, in the text_area for the 'body' field, this is the html generated:
<div class="form-group">
<label class="control-label" for="ad_body">Body</label>
<textarea class="form-control" name="ad[body]" id="ad_body" style="display: none !important;"></textarea>
</div>
For some reason, it's set to display:none. I searched anywhere in the application and couldn't find a place I set it to display:none.
Any clue?
I'm using Ruby 2.2.3 and Rails 4.2.4.
For anyone facing this issue, I managed to fix it just by changing the column name from body to ad_body. It's just that probably body is a protected term.

Rails nested model form wrong html id

I have a Cover model which has many Slots. I created a form following the #196 Railcast and #197 Railcast.
The form works fine for creation, but when I want to update a Cover (specifically a child Slot) it doesn't get updated. The problem seems to be that the html name attribute of the fields for the slot do not contain the proper ID, instead, they always seems to start at 0 for the slots and increase. Here is the relevant code:
_form.html.erb:
<%= form_for(#cover) do |f| %>
...
<%= f.fields_for :slots do |builder| %>
<%= render "slot_fields", :f => builder %>
<% end %>
...
<% end %>
_slot_fields.html.erb:
<div class="fields">
<p>
<div class="field">
<%= f.label :width %>
<%= f.number_field :width %>
</div>
...
output html:
<p>
<div class="field">
<label for="cover_slots_attributes_0_width">Width</label>
<input id="cover_slots_attributes_0_width" name="cover[slots_attributes][0][width]" type="number" value="50" />
</div>
However the slot ID is 3
Thanks in advance

How to set and unset build_association depending on contents of a text_field in Rails?

I have a simple Rails form that uses a conditional build_association (if there's not already an associated parent) to include a text_field to edit a parent. This is useful if a new record for the parent should be added. The problem with this is that if a parent already exists, if the text_field is updated then it will be changed for the parent record affecting many children. If build_association is called regardless of whether a parent association already exists, then new, duplicate parents will constantly be created.
I am trying to understand how I can use build_association and fields_for to add a new parent to the database if the user types in an unfamiliar name, but to set the parent to be an existing record if the name matches an existing parent's name. (If I can get this part to work right, the next step will be to add autocomplete.) The code is below:
<%= form_for #sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div>
<% #sermon.build_speaker unless #sermon.speaker %>
<%= f.fields_for :speaker do |g| %>
<%= g.label :name, "Speaker name:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I think my gem get_or_build will be useful for you. I had the same problem and didn't find any appropriate clean solution so I've decided to write this gem. Enjoy!

One click of the submit button in form_for results in 3 items in my database

I have a form in my rails app that creates an item in my database, but when I submit the form, it creates 3 items per click.
I have 2 other forms that add things to the same database, but they are on different pages, could that be related?
This is my form on the "new debate" page:
<%= form_for(#debate) do |f| %>
<div class="field">
<%= f.label :proposition %><br />
<%= f.text_field :proposition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I also have another form on the "show debate" page that appears twice:
<%= form_for(#debate.debates.create) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
And when I click on the submit button on any of the 3 forms, I get 3 new debates.
I think your code, might be creating those extra records.
= form_for(#debate.debates.create) do |support_form|
If my assumption is correct .debates is an association, and you are creating that association with that line.
Try using build
= form_for(#debate.debates.build) do |support_form|

Resources