My problem is that I have a form that like:
<select name="Users">
<option value=""selected>Select user to add</option>
<% notShareWith.each do |user| %>
<option value=<%user.id%>><%=user.name+" "+user.email%></option>
<% end %>
</select>
<% form_for #imageuser do |f| %>
<%= f.hidden_field :user_id ,:value => 5 %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
My drop down menu works fine and it list the users that have access. I can revoke access and everything. I have no clue how to know what option is selected from the drop down menu or how to submit that to the image users form. I am using:
<% form_for #imageuser do |f| %>
<%= f.hidden_field :user_id ,:value => 5 %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
to submit the user that is selected but I need to know what the value of the selected item is so I can pass said value into:
<%= f.hidden_field :user_id ,:value => 5 %>
Thanks again for the help.
You can also use options_from_collection_for_select
Something like this should work
<% form_for #imageuser do |f| %>
<%= f.select :user_id, options_from_collection_for_select(notShareWith, 'id', 'name'), prompt: 'Select user to add' %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
Use collection_select helper instead, something like this:
<%= f.collection_select(:user_id, notShareWith.map { |user| user.id }, :id, :id, :prompt => 'Select') %>
Then, grab the selected value like: params[:user_id]
To fill the selected id of user in your form you have to do several steps
First, add id attribute in your option
<select id="user_id" name="Users">
<option value=""selected>Select user to add</option>
<% notShareWith.each do |user| %>
<option value=<%user.id%>><%=user.name+" "+user.email%></option>
<% end %>
</select>
Then, you can use javascript to pass into imageuser form:
<script type="text/javascript">
$( document ).ready(function() {
$("#user_id").change(function(){
selected = $(this).val();
$("#imageuser_user_id").val(selected);
});
});
</script
NOTE:
You'd better use select_tag helper to create select option
<%= select_tag 'user_id', options_for_select(notShareWith.collect{ |u| ["#{u.name} #{user.email}", u.id]}, prompt: "Select user to add") %>
Related
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
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
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 a form that is submitting data to a model with nested attributes. I am using select to choose from a list of options.
The Articles model accepts_nested_attributes_for the Documents model, which has_attached_file called Attachment. The url_path of the attachment is what the select field should be selecting.
How to I change the value of the select field?
The HTML looks like this right now:
<select id="article_document" name="article[document]">
<option>...</option>
</select>
But I want this:
<select id="article_document_url_path" name="article[document][url_path]">
<option>...</option>
</select>
The Ruby looks like this:
<%= form_for #article, :html => { :multipart => true } do |f| %>
<%= f.select( :document,
options_for_select(#article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }) ) %>
<% end %>
Rails 4.1.8, ruby 2.1.5p273
Try this:
<%= form_for #article, :html => { :multipart => true } do |f| %>
<%= f.fields_for :document do |doc|%>
<%= doc.select(
:url_path,
options_for_select(
#article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }
)
) %>
<% end %>
<% end %>
I am trying to access the value of an element in a different form. More specifically I have two forms, one normal html form which submits the information, and an ajax form that updates a field in the database. I want the ajax form to take the value of an element in the normal form, but I have no idea how to accomplish that. Here is my current setup:
Message View
<%= form_for #message do |f| %>
<%= f.text_field :to %>
<%= f.text_area :body %>
<%= f.submit "Send Message" %>
<% end %>
<%= form_for :save, :url => save_message_path(), :remote => :true do |f| %>
<%= f.submit "Save" %>
<% end %>
Message Controller
def save
account.message = params[:body]
end
But the :body param is in the other form so it sets account.message to nil because the ajax form did not have a body parameter. I would like it so the ajax form can submit the value of the body element.
Since the form is edited on the client side, you'll have to write this functionality in javascript.
It is relatively straightforward to copy the value of the message body into a hidden field and submit the Ajax form. This would be most easily accomplished by adding an id your ajax form as follows:
<%= form_for :save, :url => save_messages_path(), :remote => true, :id => 'ajaxform' %>
<%= hidden_field_tag 'hidden_message' %>
Then in javascript you could do the following:
$(document).ready(function(){
$("#ajaxform").submit(function() {
$("#hidden_message").val($("#message_body").val());
return true;
});
}
You can add a hidden field body in your ajax form, and use an onsubmit event to populate it with the value of the body field from the normal form before the ajax request is sent. It would look more or less like this:
<%= form_for #message do |f| %>
<%= f.text_field :to %>
<%= f.text_area :body, :class => 'main_body_field' %>
<%= f.submit "Send Message" %>
<% end %>
<%= form_for :save, :url => save_message_path(), :remote => :true, :id => 'ajax_form' %>
<%= f.hidden_field :body, :class => 'hidden_body_field' %>
<%= f.submit "Save" %>
<% end %>
javascript (with jQuery):
$(function() {
$("ajax_form").submit(function() {
// copy body value from first form to the second one
$('.hidden_body_field').val($('.main_body_field').val());
return true;
}
}