How to get value in controller of an bootstrap-multiselect-rails - ruby-on-rails

I'm new to rails and web. I use the 'bootstrap-multiselect-rails' gem to create a select box and normal inputs.
When I click the submit button, I don't retrieve the select box informations in the params variable (the inputs are well retrieve).
This is probably due to the bootstrap-multiselect gem that overrides the behavior of the select box by replacing it with a ul,li system.
%select#select-type{:multiple => "multiple"}
- #list_of_type.each do |type|
%option{value: "#{type.label}"} #{type.label.upcase_first}
I have seen answers that explains in retrieving the information in javascript but how to perform treatments on its information in the controller?

In your select field I cannot see that you assigned a param to it. Usually select goes like this: <%= select :input, etc... where :input is the param you want to save into your model.
Rails docs give following example:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true })
So the problem is that you did not specify the param. It is also not empty, it is not mentioned at all in your Parameters. Correct?! Than please check the basic docs again on how to create a select field and add the param as requested. Unfortunately, I do not know the exact syntax using your gem.
I am also not sure about the :multiples=>"multiple". or should it be multiple: true? But I think your version might work with the gem. However, your haml only creates the HTML select with #id and multiple: true. But what you want is something like this
<select name="post[category]" id="post_category">
So that the param is clear in the select.
Afterwards, when you have saved the param you can use it also in your controller action.
In conclusion: Add the param to your select field. I don't know how to do this with the gem but you might find some docs may be, or check basic rails docs which might help you out here for sure. Also SO has many questions on select with rails. Good luck!

Can you show the rails code for the multi form? You just need to add a value to each select option which will be stored by rails if you have permitted the params in controller and set up a column for it migrations.
If questions on how to add value to select_tag check out the rails docs. In your html markup i cannot see if the options have value.

Related

Rails: Datagrid enum filter does not work when multiple or checkboxes options are true

I have a form that is using Datagrid for searching on a single table. The filters and searching work as expected, including this one:
filter(:location, :enum, :select => :locations)
However, when I try to make that filter show up as checkboxes, like so...
filter(:location, :enum, :select => :locations, :checkboxes => true)
...the checkboxes show up but the filter no longer works, and it returns the result set as though I never checked any of the checkboxes at all. This same unexpected behavior also happens when I try to use the :multiple option. According to the Datagrid gem documentation I don't see any other requirements for making a filter work with checkboxes or multiple select options.
When viewing the requests on my local server, I noticed that the location filter is not being added to the SQL query when :multiple or :checkboxes is set to true, but the values ARE being passed in the query string, like so (indicated by FBR and FBZ):
http://localhost:3000/searches/index?utf8=✓&searches_grid[name]=Foo&searches_grid[location][]=FBR&searches_grid[location][]=FBZ&commit=Search#searchAnchor
Here's what the working example (no multiples and no checkboxes) looks like:
http://localhost:3000/searches/index?utf8=✓&searches_grid[name]=Foo&searches_grid[location]=FBR&commit=Search#searchAnchor
I'm not sure if Datagrid is having trouble rendering parameters that are sent as arrays, or if there's some kind of issue with my versions of Rails/Ruby/etc, or what's going on. If anyone has any suggestions or workarounds I would greatly appreciate them, and if not I will try submitting a Gitlab issue for the gem.
I am using Datagrid 1.5.4, Rails 5.0.3, and Ruby 2.3.1p112.
After hours of dissecting everything in my code, I noticed that the location parameters of the search_params action in my controller was missing the square brackets to allow it to contain multiple values:
params.require(:searches_grid).permit(
:name,
...more params...
:order,
:descending,
location: [] # <-- Had to add brackets here
)
Now it's working as expected.

Rails simple_simple form, feature test failing to populate field/sometimes

I have a finicky test(s).
I'm using simple_form and rails to create a pretty standard job/college type application for users to fill out.
Issue is: Sometimes in the feature test, when a user is editing the application(created via Factoryfirl), rails/simple_form fails to populate a datefield with some of the variables of the date. This also seems to only happen when include_blank: is true.
I know it's being created with a date. The field is failing to be populated with the date, sometimes and only the year column.
I don't think this is an issue with anything I wrote as the tests do work sometimes. Could be a config issue so:
rails-4.2.5.1
rspec-core-3.3.2
capybara-2.5.0
the call to the field:
= required_field_on_submit(f, :profile, :birthdate, as: :date, start_year: years_ago(100), end_year: years_ago(Application::MINIMUM_AGE - 1), include_blank: true, order: order(:m, :d, :y))
required_field_on_submit is just a custom wrapper for f.input, bc of the way applications are being validated. Nothing funky happening there, already checked.
As I mentioned in the comments - A usual cause of a date select not being correctly filled by a page during tests is the data being generated (factory, etc) being outside the acceptable range of the date select
Are you able to select the value for which is given by (years_ago(100)) from front end manually? If yes then make sure that the page is loaded properly, before passing the values. You can wait for an element or verify a message on the page.
You can also try debugging the failing cases, by adding "puts" statement to print the date in the console, so that it can be verified that whether the date is in correct range or not.

Select tag & options for select 101

Can someone explain me how this exactly works ?
Problem:
I have a Scaffold and run the migration:
rails g migration AddRarityToTags rarity:string
For the rarity input i need a dropdown displaying a list of options to select from.
e.g. Rarity = Free
Common
Rare
Epic
If i'm right i need something like this:
select_tag :rarity, options_for_select(#rarity)
I searched a lot but it didn't helped much, i got more confused.
Can someone help me out ?
Imagine putting the raw options into the tag as a string:
select_tag :rarity, '<option>Free</option><option>Common</option>...'
That's what options_for_select returns, if you pass in an array:
select_tag :rarity, options_for_select(['Free', 'Common', ...])
See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
To enforce the "dumb views" rule, and to let others use that array, you can move it to the model:
class Tag < ActiveRecord::Base
RARITY_LEVELS = %w(Free Common Rare Epic)
end
...
select_tag :rarity, options_for_select(Tag::RARITY_LEVELS)
Both me and the OP would like to know if Rails, or any gems, let us get any DRYer than this; if, for example, Rails lets us attach the RARITY_LEVELS directly to the rarity field...

Pass values of checkboxes into an array?

Im trying to update someone else's Rails app. Right now, an HTML table displays values from a database. What i want is to be able to display a checkbox for each row and on submit, the values of the checkboxes are sent into an array (and the values shouldnt be "checked" or "unchecked", they should be the id's of the database row).
Heres what i have so far.
Checkbox : (message.id being a dynamic id)
<%= check_box_tag "message_ids[]", message.id %>
And on the controller:
#dispatches = Dispatch.find_by_message_ids(CODE TO RETRIEVE CHECKBOX ARRAY GOES HERE)
Any suggestions?
Have you tried inspecting the value of params?
Chances are this will work:
#dispatches = Dispatch.find_by_message_ids(params[:message_ids])
But if it doesn't, just look at what is being sent to your page. Try one of these:
logger.info(params)
or
raise (params.inspect)
or
render :inline => params.to_yaml
Check what you receive in your params because they're probably there if this is defined correctly. The current parameters are always logged in log/development.log which is something you should have open any time you're debugging something.

Select prompt option disappears when validation fails in Rails

Let me preface by saying I'm a noob to Rails and StackOverflow so please go easy on me. I'm using Rails 2.3.8 with sqlite3 on my dev box.
I have created a select pulldown in a form using the following:
<%= select( "communication", "gig_id", { "Add New Gig" => "new"}, {:prompt => "-- Select Gig --"}, :onchange => "toggle(this, 'gigInfo')") %>
However, when something else in the form fails validation and the "new" page is re-rendered, my prompt goes away and the only option left is the "Add New Gig" option. This is the case with ALL my forms and I can't seem to find any answer as to why.
My controller uses the basic scaffolding so I'm sort of at a loss. Any help would be greatly appreciated.
Your observation is accurate, and you astutely observed that :include_blank remains even when :prompt does not.
You can achieve the results you want by setting :include_blank to the string you were using for prompt (it doesn't need to be a boolean).
:prompt, it seems, only appears when there is no value for Rails to supply the given field. When your app re-renders the new view, there is a value to supply that field because it created an instance of your Communication model. (It failed to save that instance, but it looks as though that instance has its gig_id field set.)

Resources