I have a search_form_for and I need to be able to select multiple Payment statuses to make a Ransack search.
We already have an f.select dropdown box for this attribute, but we now need to be able to select more than one status at the same time.
form:
<%= search_form_for #search, url: admin_payments_path, html: {class: "form-inline"} do |f| %>
<div class="form-group">
<%= f.select :status_eq, payment_status_selector, { include_blank: "Payment status.."}, class: "form-control gray" %>
</div>
<% end %>
I've tried:
<%= f.select :status_eq, payment_status_selector, {include_blank: false}, {multiple: true, as: :radio_buttons} %>
gives me a select box with all the options allowing me to select multiple, but they are not radio_buttons and an empty value "" is passed along with the selected options.
<% payment_status_selector.each do |status| %>
<%= radio_button_tag :status_eq, "#{status[1]}", false, class: 'radio-checkbox' %>
<%= label_tag( "#{status[0]}") %>
<% end %>
This gives me a radio_button for each possible status, but I can't select more than one.
I can't figure out the correct way to do it. Maybe with check_boxes are a better option to be able to select/unselect statuses?
Yes, using check_box will be a better option.
Try,
<% payment_status_selector.each do |status| %>
<%= f.check_box :payment_statuses, { multiple: true }, status, false %>
<%= label status %>
<% end %>
And you can expect the values in controller from params as:
{ search: { payment_statuses: ["status 1", "status 2"] }
make sure that payment_status_selector returns array of values.
Hope that helped.
Reference: https://medium.com/programming-problem-solving-logbook/rails-multiple-checkboxes-e9c4c7fda356
Related
I added a new column to model Plan, named :per_unit_quantities_configuration which is a hash with min, max and step key/values.
t.jsonb "per_unit_quantities_configuration", default: {}
When I edit a Plan, the hash is being correctly saved to the DB (I can access each key/value from the console), but the forms are not displaying any of its values (the fields are empty).
I tried adding a store_accessor for the column in the Plan model, but it is not working:
store_accessor :per_unit_quantities_configuration, :min, :max, :step
Example of a simple_form html that does not display hash values:
<%= simple_form_for [:admin, #base_plan, #plan] do |f| %>
<% if f.object.base_plan.per_unit? %>
<div class="d-flex">
<%= f.simple_fields_for :per_unit_quantities_configuration do |fields| %>
<% if f.object.errors[:per_unit_quantities_configuration].any? %>
<%= f.error :per_unit_quantities_configuration, id: "per_unit_price_error", class: "invalid-feedback", error_prefix: "gato" %>
<% end %>
<%= fields.input :min %>
<%= fields.input :max %>
<%= fields.input :step %>
<% end %>
</div>
<% end %>
<%= f.button :submit, class: "pull-right" %>
<% end %>
What am I doing wrong?
since you setup store_accessor :per_unit_quantities_configuration then you can access directly 3 attributes min, max, step, so that you no need to wrap those attributes on simple_fields_for :per_unit_quantities_configuration and treat them as normal fields (that mean on controller you have to permit them as normal fields)
# view
<%= f.input :min %>
<%= f.input :max %>
<%= f.input :step %>
# controller
def plan_params
params.require(:plan).permit(:min,:max,:step)
end
Ok, so here is my code.
<%= form_for #quiz do |f|%>
<% #questions.shuffle.each do |q| %>
# get_answers returns an array of 4 random answers
<% answers = q.get_answers %>
<%= f.label q.inquest %><br>
<% answers.each do |a| %>
<%= f.radio_button <need help here!>, a %>
<%= f.label <need help here!>, a %><br>
<%= f.hidden_field <need help here!>, q.id %>
<% end %>
<% end %>
<% end %>
my quiz table has the attributes
concept_id
user_id
how do I add custom attributes that I can have access to in the post action? It should be dynamic so it can scale with how many questions there are. I want something like this for my params.
{
user_answers: {
question1: {
:question_id
:answer
{
}
}
Depending on how many questions there are, it'll go like question1, 2, 3, 4 etc..
The hidden_field would carry with question_id while the radio_button would carry the answer. I don't need it to save or anything, I just need access to them
Ok, so I realized that I shouldn't have called those form tags on f. instead have them as their own form tags
for example:
radio_button_tag instead of f.radio_button
Would you know how to make a pull down or drop down menu for a form text field?
Currently, the user needs to to type in text "Rating" then submit the form. But ideally, they should select a rating of "Awesome" "Good" or "Meh" from a pull down, rather than be able to enter a custom text. Any advice in the right direction would be appreciated.
Thank you.
_form.html.erb
<%= simple_form_for(#post) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :Duration, required: true, autofocus: true %>
<%= f.input :Rating %>
</div>
<div class="form-actions">
<%= f.button :submit, "Log it!" %>
</div>
<% end %>
Please try to replace the line:
<%= f.input :Rating %>
with:
<%= f.select :Rating, options_for_select(["Awesome", "Good", "Meh"]) %>
I have a pair of radio buttons that I want to pre-assign a checked value to only my new action. Right now I'm conditionally rendering two partials. One partial that has radio buttons with checked attributes and the other with not attributes at all:
<%= form_for([#restaurant, #dish_review], url: :restaurant_dish_reviews) do |f| %>
<% if action_name == "new" %>
<%= render "status_buttons_checked", f: f, dish: #dish %>
<% else %>
<%= render "status_buttons", f: f %>
<% end %>
<% end %>
_status_buttons_checked
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
_statsus_buttons
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
I was wondering if there was any way in Rails where I can insert the conditional in the radio_button parameter instead of creating two partials. I'd like to something similar to what's show below but run into a template error:
<%= f.radio_button :status, :downvoted, if action_name == "new" current_user.voted_down_on?(dish) %>
When using form_for, the form methods you use are automatically populated with the appropriate data for your attributes. Whilst I don't know if this works with the checked value, it means that if you have the following:
<%= form_for #user do |f| %>
<%= f.text_field :name %>
<% end %>
... :name will be populated from your #user object (if it's new, no data will be inserted).
--
This means that if you're using form_for, you should be able to populate the checked value with the data passed to the view:
<%= form_for [#restaurant, #dish_review] do |f| %>
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on? #dish %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on? #dish %>
<% end %>
I don't see what you're trying to achieve from your partials (they're both the same) -- but if you wanted to create "checked" on conditions, you'd be able to use the following:
<%= checked = action_name == "new"
<%= f.radio_button :status, :downvoted, checked: checked %>
This will set the value as "true" or "false" depending on whether the action is new or not.
I have excel, video, and multiple choice boolean columns in my Step model. I am trying to get it so the user has to choose one of the three, and when saved it passes back to the database as true. Right now my two problems are a)when I select one, it doesn't deselect the others and b) the radio button is on a different line than the text that labels it. Any help would be appreciated.
<fieldset class="stepCreator">
<%= "Step" %>
<%= f.label :description, "Description" %>
<%= f.text_field :description %>
<div>
<%= f.label :excel, "Excel" %>
<%= f.radio_button(:excel, true, :checked => true) %>
<%= f.label :video, "Video" %>
<%= f.radio_button(:video, true) %>
<%= f.label :multiple_choice, "Multiple Choice" %>
<%= f.radio_button(:multiple_choice, true) %>
</div>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "btn btn-danger btn-mini remove_fields "%>
</fieldset>
Actually you have 2 very different problems.
All the radio buttons (that you want to select one and automatically deselect the others) must have the same name, and different values, so it should be something like:
f.radio_button(:media, :excel, :checked => true)
f.radio_button(:media, :video)
For the labels to be at the same line as the checkbox, you just have to change the CSS.
From the documentation:
http://guides.rubyonrails.org/form_helpers.html
try:
<%= radio_button_tag(:age, "child") %>
<%= label_tag(:age_child, "I am younger than 21") %>
<%= radio_button_tag(:age, "adult") %>
<%= label_tag(:age_adult, "I'm over 21") %>
radio buttons have to select the same attribute in your model, in this example i selected the age attribute and i can give it 1 of two values, or he is a child or he is an adult
hope it helps