Submit button not saving/updating changes Ruby on Rails - ruby-on-rails

I have this form:
<fieldset data-model="idea-art" class="idea-edit">
<h2>Artwork Specs</h2>
<%= form_for #idea do |f| %>
<div data-attribute="art_status" class="edit-field">
<%= f.label :art_status, "Art Status" %>
<%= f.select :art_status, Idea::ART_STATUSES %>
</div>
<div data-attribute="artist" class="edit-field">
<%= f.label :artist_id, "Artist" %>
<%= f.select :artist_id, User.artists.collect{|x|[x.full_name, x.id]}, :include_blank =>
true %>
</div>
<div data-attribute="default_size" class="edit-field">
<%= f.label :default_artwork_id, "Default Artwork" %>
<%= f.select :default_artwork_id, #idea.artworks.collect{|x|[x.dimensions, x.id]},
:include_blank => true %>
</div>
<div data-attribute="colors_offered" class="edit-field">
<%= f.label :colors, 'Colors Offered' %>
<%= f.collection_select :color_ids, Color.master_colors.order(:name), :id, :name, {},
{multiple: true}%>
</div>
<div data-attribute="base" class="edit-field">
<%= f.label :base, "Base" %>
<%= f.check_box :base %>
</div>
<div data-attribute="special_instructions" class="edit-field">
<%= f.label :special_instructions, "Special Instructions" %>
<%= f.text_area :special_instructions %>
</div>
<div data-attribute="artwork" class="edit-field">
<%= f.fields_for :artworks do |artworks_f| %>
<%= render 'artwork_fields', f: artworks_f %>
<% end %>
<p>
<%= link_to_add_fields "Click To Add Artwork", f, :artworks %>
</p>
</div>
<%= f.submit %>
<%end%>
</fieldset>
when I click submit my changes are not updated. I have tried many things and nothing seems to work. I am new to ruby and have read the RailsGuide to form helpers and I still can't quite figure it out.
Is there something I am missing?
Thanks

Do you see the request in Rails server log? If so, look for a Rollback in the log, which might indicate that ur models are invalid, perhaps due to strong parameters settings. If the request is not making it to the server, inspect for javascript errors on the page, then make sure you're not using (or intend to use) ajax requests, in which case you might need to require jquery_ujs in application.js along with csrf_meta_tag in you layout.

Related

How to autofill select tag in Rails 5?

I am using a Rails app and am trying to get a select field to work properly.
I currently have in my _form.html.erb:
<%= form_with(model: rec, local: true) do |form| %>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description, id: :rec_description, :size => 100 %>
</div>
<div class="field">
<%= form.label :list_id %>
<%= form.select 'list_id', options_for_select(#lists.collect{ |u| [u.name, u.id] })%>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
However, when I go to my edit view, the select field automatically defaults to the first choice in the field. I'd like it to remain what it was previously. Otherwise, the user will have to go back each time and change it.
I tried
<%= form.select 'list_id', options_for_select(#lists.collect{ |u| [u.name, u.id] }, :selected => form.list_id %>
Howevever, I get an error saying: undefined method `list_id'
How should I address this?

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

Handle a B model inside the view's form of an A model

I have a classic form_for:
<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>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
I would like to use another model inside this view, that would also update when the user clicks the submit button. I know this has to do with nested forms but I'm a little confused about how to implement it. Here's the variable from the second model that I would like to add:
<% #trip_distances.each do |t| %>
<%= form_for(t) do |e| %>
<div class="field">
<%= e.text_field t.id_contract %>
<%= e.number_field t.length %>
</div>
<% end %>
Obviously this is not correct. I guess I need to use the field_for method?
Firstly, consider using simple_form gem, as it cleans up a bit the code, and is overall more developer-friendly :)
Then in your view you will have something like
<%= simple_form_for sth do |form| %>
<% form.input :attributeUno %>
<% form.input :length %>
...
Then to add to this form inputs for nested element add simple_fields_for
<% #trip_distances.each do |t|
<%= form.simple_fields_for t do |fields| %>
<% fields.input :length %>
...
And don't forget to add
accepts_nested_attributes_for :something in your model :)
PS: You should also consider using slim or haml to make creating views a bit easier, and also clean them up :)

undefined method for form fields

I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.

Submit button not working in Ruby/Firefox

I'd like to have a single form that has 2 or more field-set_tags and I'd like to have a single submit button to update the changes for all field_set_tags at once, but that's not working ; if I create a form for each field_set_tag and one submit button per form, it works fine ; anyone can help? my simplified partial view _form.html.erb has the following structure
<%= form_for(#my_form) do |f| %>
<%= field_set_tag "Field Set A" do %>
<div class="field>
<%= f.label :"Configure A" %>
<%= f.check_box :ConfigA %>
<%= f.label :"Login" %>
<%= f.text_field :A_Login, {:size => 12} %>
<%= f.label :"Password" %>
<%= f.password_field :A_Password, {:size => 12} %>
<!-- more Ruby code here -->
</div>
<% end %>
<%= field_set_tag "Field Set B" do %>
<div>
<%= f.label :"Configure B" %>
<%= f.check_box :ConfigB %>
<%= f.label :"Login" %>
<%= f.text_field :B_Login, {:size => 12} %>
<%= f.label :"Password" %>
<%= f.password_field :B_Password, {:size => 12} %>
<!-- more Ruby code here -->
</div>
<% end %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<!-- more field_sets and Ruby code here -->
<% end %>
I also tried this with no luck either
<input type="submit" value="Update" />
My issue was caused by a button I configured as a place holder for future use like this
<div class="field">
<%= button_to "Advanced" %>
</div>
When I removed that button, all worked fine
In a similar way, I configured 2 successive submit buttons like this
<div class="actions">
<%= button_to "Update", :type => "submit" %>
</div>
<div class="actions">
<%= button_to "Update", :type => "submit" %>
</div>
the 1st one does submit but the 2nd one generates this error
Unknown action
The action '4' could not be found for L2CircuitTestsController
2 or more successive "Advanced" buttons cause the same behavior : only the 1st one does the job and the other are eclipsed!

Resources