I have two models: Meal and Tag. They are associated by HABTM. What I want to do is to add new meal. So I have new.html where is name field, preparation field, and all tags (represented by toggle-buttons - every tag has own toggle-button). User can clicked tags, which want to save in this meal.
And here is a problem. I can display all tags, but I have no idea how can I tell rails to save only clicked tags.
Could you help me find right approach?
oh, and there is a user too (user has many meals), but I thing it doesn't matter.
Here is my view:
<%= form_for(#meal,:html => {:class => "meal-data-form"}) do |f| %>
<%= render 'shared/error_messages', object: f.object%>
<%= f.label :name, "Nazwa *" %>
<%= f.text_field :name, class: 'form-control'%>
<%= f.label :preparation, "Sposób przyrządzenia" %>
<%= f.text_area :preparation, class: 'form-control' %>
<h3>Określ tagi dla tego posiłku</h3>
<div id="tags-associated-with-meal">
<h4>Rodzaj posiłku</h4>
<div id="associated-nutrient-tags">
<% current_user.nutrient_tags.each do |nutrient_tag| %>
<button id="associated-type-tag-<%= nutrient_tag.id %>" type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
<%= nutrient_tag.name %>
</button>
<% end %>
</div>
</div>
<%= f.submit yield(:button_name), class: "btn btn-primary" %>
I would not use a toggle button to do this, I would probably use checkboxes.
This is what you would do if you used checkboxes:
<%= f.collection_check_boxes :nutrient_tags_ids, current_user.nutrient_tags, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
Here is something for reference: http://www.sitepoint.com/save-multiple-checkbox-values-database-rails/
And here is the Rails collection_check_boxes official documentation: http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
Hope that helps.
Related
I have a basic question about checkboxes in forms.
I have a simple form for user interests, which has two fields - id and interest_id
<%= simple_form_for (#user_interest) do |f| %>
<%= f.error_notification %>
<%= f.label :interest_id %><br>
<%=f.text_field :interest_id, class: 'form-control'%>
<%= check_box_tag :interest_id %>
<%= f.submit "Update my account", class: "btn btn-primary" %>
<% end %>
If I look at the resulting HTML
<input class="form-control" type="text" name="user_interest[interest_id]" id="user_interest_interest_id">
<input type="checkbox" name="interest_id" id="interest_id" value="1">
I am having difficuly understanding why the the name of the text_field (which works) is correct, but the name of the checkbox (which doesn't work) is "interest_id" (and not user_interest[interest_id]
Fully appreciate this is completely basic but actually, these very basic questions can be difficult to get answers to. Many thanks.
Simple fix,
<%= f.label :interest_id %><br>
<%= f.text_field :interest_id, class: 'form-control'%>
<%= f.check_box :interest_id %>
I presume the check box was not getting created as part of the form object.
In BootStrap, I can use the following code to get a button with an icon.
<button class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Click Me</button>
Now I want to do the same thing for the submit button of a Rails form. I don't know how to do it and below is my code:
<h1>Create a post</h1>
<%= form_for :post, url: posts_path do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
<p class="help-block">Please type the title of the post</>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: "form-control", rows: 5 %>
<p class="help-block">Please type the body of the post</>
</div>
<%= f.submit class: "btn btn-primary" %>
<% end %>
Could someone give me any suggestions?
I found the solution here: HTML code inside buttons with simple_form
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
Text
<% end %>
I have a form that posts something to a feed. I would like an user to post to the feed if they are signed in, but if they are not and they hit submit, I would like them to see the login modal. Here is my current form without this logic. It's a basic form_for - nothing special.
<%= form_for(#sub_opp) do |f| %>
<div class="field">
<%= f.label :sport, :class=>'form_label' %>
<%= f.select :sport, ['Basketball', 'Beach Volleyball', 'Flag Football'] %>
</div>
<div class="field" id = "datetime_select">
<%= f.label :Sub_Date, :class=>'form_label' %>
<%= f.datetime_select :sub_time, :ampm => true, :minute_step => 15 %>
</div>
<div class="actions">
<%= f.submit 'POST', :class=>"btn btn-success btn-large" %>
</div>
<% end %>
Conceptually, I would like to incorporate logic in along these lines when one clicks the submit button.
<% if current_user.present? %>
Submit the form
Otherwise
<a href="#myModal" data-toggle="modal">
Can you do this with a form_for? Thanks
I figured it out with a simple if / then statement. Surprised i did not think of this earlier. Thanks.
<% if current_user.present? %>
<div class="field">
<%= f.hidden_field :poster_user_id, :value => current_user.id %>
</div>
<div class="actions">
<%= f.submit 'POST', :class=>"btn btn-success btn-large" %>
</div>
<%else%>
<div class="actions">
POST
</div>
<%end%>
I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.
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!