if #challenge.name == 'foo'
#challenge.category = 'habit'
#challenge.days_challenged = 21
#challenge.why = 'bar'
else
:days_challenged & :why are properly being set in the _form for foo but not :category.
<div class="challenge-category">
<input class="date-format-switcher" type="radio" value="goal" name="challenge[category]" id="challenge_category_goal">
<label for="challenge_category_goal">Goal</label>
<input class="date-format-switcher" type="radio" value="habit" name="challenge[category]" id="challenge_category_habit">
<label for="challenge_category_habit">Habit</label>
</div>
<%= f.number_field :days_challenged, class: 'day-challenge' %>
<%= f.text_area :why %>
I assume you are purposefully custom-coding your radio buttons. The selected radio button should have the attribute "checked", i.e.
<input class="date-format-switcher" type="radio" value="habit"
name="challenge[category]" id="challenge_category_habit" checked />
To set dynamically:
<%= f.radio_button :category, 'goal', class: 'date-format-switcher' %>
<%= f.radio_button :category, 'habit', class: 'date-format-switcher' %>
Related
I am new in RoR. I am trying to add css classes on my form. Here is my form:
<%= form_tag :action => 'create' do %>
<p><label for = "name">Name</label>:
<%= text_field 'books', 'name' %></p>
<div class="form-control">
<p><label for = "author">Author</label>:
<%= text_field 'books', 'author'%></p>
</div>
<p><label for = "price">Price</label><br/>
<%= text_area 'books', 'price'%></p>
<%= submit_tag "Create" %>
<% end -%>
That form controls does not accepting class="form-control mb-4 col-10" placeholder="Patient name" like <input type="text" th:field="*{name}" class="form-control mb-4 col-10" placeholder="Patient name">. How can I apply css styling.
You have to put the class behind a comma
<%= text_field 'books' :name, class:"form-control mb-4 col-10", placeholder="Patient name" %>
How can I call a method about a form?
I have 3 radio buttons and I want to call a method depending on which radio button is checked. After the call I have to return the controller a variable to the view.
How can I do that?
My approach:
I want use CSS do design my HTML. That is the reason why I haven't used a form helper:
<div id="cx" class="cxs">
<form>
<fieldset>
<label>
<input type="radio" id="cxId" name="checkBox"> ID
</label>
<label>
<input type="radio" id="cxName" name="checkBox"> Name
</label>
<label>
<input type="radio" id="cxArtikelnummer" name="checkBox"> Artikelnummer
</label>
</fieldset>
<button type="button" id="cxBtn">Ok</button>
</form>
</div>
Here I want to call a method depending on which radio button is checked (show by name, id or artikelnummer).
I have solved it stati, but the choose have to be dynamic and I thought do that with methods.
<% $i = 0 %>
<% $num = #title.count %>
<% #title.each do |row| %>
Name: <%= row["Name"] %>
Durchlauf: <%= $i %> <br>
<% $i += 1 %>
<% end %>
If #title represents a model in which there is for example "aaa" field of string type which can have the "ID" value, "Name" value and "Artikelnummer", try use this:
<% #title.each do |row| %>
<% if row.aaa == "ID" %>
#code for ID option
<% elsif row.aaa == "Name" %>
#code for Name option
<% elsif row.aaa == "Artikelnummer %>
#code for Artikelnummer option
<% end %>
<% end %>
That is my idea. Tell me if it will help you.
But I think you can use form_for helper and radio_button helper inside with its :checked option and the whole form_for you can design via CSS via classes for every <li class="sth"><%= f.radio_button "Name", checked: true%></li> but I should check if this solution is right.
Rgds,
Marta
For some reason when executing this code, the radio buttons that get created aren't mutually exclusive in getting picked. How do I make it so that users can only pick 1 radio button? This code is essentially going through a variety of addresses and allowing a user to pick an address for their order.
<div class="select-address select-address-row" style="display:none">
<% #order.user.addresses.each do |address, index| %>
<%= form_for #order, remote: true, :html => { :id => 'address-form-'+address.id.to_s} do |a| %>
<div class="col-xs-4 select-address-col">
<div class="enterprise-buy-address-box address-<%= address.id %>">
<%= address.shipping_name %><br>
<%= address.line_1 %><br>
<%= address.city %>, <%= address.state %> <%= address.zipcode %><br>
<% isDefault = address.shipping_name == #defaultAddress.shipping_name ? true : false %>
<%= a.radio_button :pickedAddress, 'Address', :checked => isDefault %>
<%= label :pickedAddress, '' %>
<div class="submit-address">
<%= button_tag(type: 'submit', class: "btn btn-xs btn-default address-custom-button-select address-"+address.id.to_s+"-button") do %>
select
<% end %>
</div>
</div>
</div>
<%= a.hidden_field :address_id, :value => address.id %>
<% end %>
<script>
$('.address-<%=address.id%>-button').click(function() {
$('.default-address').empty();
var selectedAddress = $('.address-<%=address.id%>').clone();
selectedAddress.find('.submit-address').remove();
$('.default-address').append(selectedAddress);
$('default-address').addClass("enterprise-buy-address-box");
$('.default-address').show();
$('.select-address').toggle();
$('.change-address-link').toggle();
});
</script>
<% end %>
</div>
Your form_for tag is inside of the loop for the addresses. Even if the name attribute is the same for all of your input type="radio" elements, they will not be mutually exclusive across different form elements. Move your form_for tag outside of the loop so that the radio inputs belong to the same form.
See the following snippet for demonstration:
<p>These won't work</p>
<form>
<input type="radio" name="foo">
</form>
<form>
<input type="radio" name="foo">
</form>
<p>These do work</p>
<form>
<input type="radio" name="bar">
<input type="radio" name="bar">
</form>
i got a form which looks like this:
<h1>Title: <%= #task.title %></h1>
Dates: <% #dates.map do |date| %>
<%= form_tag("/responses/create/", :method => "post", :id => #task) do %>
<%= date.task_date %> <%= check_box_tag "response_checkbox", date.task_date %>
<%= submit_tag("Add") %>
<% end %>
<% end %>
what i want is a list of checkboxes where the user can select one and then submit the form with one button. but the form produces one submit button for every object and i can't figure out why! working on this for hours now ...
here is the html output:
<h1>Title: Weihnachtsball</h1>
Dates:
<form id="#<Task:0x000001067a6320>" method="post" action="/responses/create/" accept-charset="UTF-8">
<div style="display:none">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="OaDIn2B0yHVBAY9z7F0IOdiULu5GiqlSJV5+Ft92tbw=" name="authenticity_token">
</div>
2014-11-10 11:11:00 UTC
<input id="response_checkbox" type="checkbox" value="2014-11-10 11:11:00 UTC" name="response_checkbox">
</form>
<form id="#<Task:0x000001067a6320>" method="post" action="/responses/create/" accept-charset="UTF-8">
<div style="display:none">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="OaDIn2B0yHVBAY9z7F0IOdiULu5GiqlSJV5+Ft92tbw=" name="authenticity_token">
</div>
2014-11-23 12:14:00 UTC
<input id="response_checkbox" type="checkbox" value="2014-11-23 12:14:00 UTC" name="response_checkbox">
</form>
<input type="submit" value="Add" name="commit">
</div>
</div>
you need to iterate through dates inside the form_tag
<h1>Title: <%= #task.title %></h1>
<%= form_tag("/responses/create/", :method => "post", :id => #task) do %>
Dates: <% #dates.map do |date| %>
<%= date.task_date %> <%= radio_button_tag "response_checkbox", date.task_date %>
<% end %>
<%= submit_tag("Add") %>
<% end %>
should be ok.
Update
I think radio_button_tag is better for you case. After all you want the user to select one box.
I am trying to create a form using the following view file
<%= form_for :spec do |form| %>
<fieldset>
<legend><%= #title %></legend>
<%= text_field_for form, "first_name" %>
<%= text_field_for form, "last_name" %>
<div class="form_row">
<label for="gender">Gender: </label>
<%= radio_button :spec, :gender, "Male" %> Male
<%= radio_button :spec, :gender, "Female" %> Female
<%= radio_button :spec, :gender, "Other" %> Other
</div>
<div class="form_row">
<label for="birthdate">Birthdate:</label>
<%= date_select :spec, :birthdate,
:start_year => Spec::START_YEAR,
:end_year => Time.now.year,
:include_blank => true,
:order => [:month,:day,:year] %>
</div>
<%= text_field_for form, "occupation" %>
<%= text_field_for form, "city" %>
<%= text_field_for form, "state" %>
<%= text_field_for form, "zip_code", Spec::ZIP_CODE_LENGTH %>
<%= submit_tag "Update", :class => "submit" %>
</fieldset>
<%end%>
And I am using the following helper method
def text_field_for (form, field,
size=HTML_TEXT_FIELD_SIZE,
maxlength=DB_STRING_MAX_LENGTH)
label = content_tag("label","#{field.humanize}:", :for => field)
form_field = form.text_field field, :size => size, :maxlength => maxlength
content_tag("div", "#{label} #{form_field}", :class => "form_row")
end
However, my output gives HTML code for
<label for="first_name">First name:</label> <input id="spec_first_name" maxlength="255" name="spec[first_name]" size="15" type="text" />
<label for="last_name">Last name:</label> <input id="spec_last_name" maxlength="255" name="spec[last_name]" size="15" type="text" />
instead of textfields and labels
Can anyone suggest a solution for this?
Update text_field_for method as below:
def text_field_for (form, field,
size=HTML_TEXT_FIELD_SIZE,
maxlength=DB_STRING_MAX_LENGTH)
label = content_tag("label","#{field.humanize}:", :for => field)
form_field = form.text_field field, :size => size, :maxlength => maxlength
content_tag "div", label + form_field, :class => "form_row"
end
You were treating the label and form_field as String. You shouldn't be interpolating them.