Problems acessing parameter from controller (collection_select) - ruby-on-rails

I'm using a collection_select to pass the param product_id from a view to the controller, but i'm having problems on acessing that value. The other parameters are fine. If i do something like aux = params[:product_id] it saves the value 0 instead of 1, that is the value that the controller receives as you can see in the request log. Any help would be appreciated!
PS: I think it may be related with the curly brackets you can see around the product_id param as you can see in the request log
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"hE4qrSZnI8LLy6sNzR2fkRxKZpFoOHZLun6Z/cIsHDxGcCaC+zVPLk1qYFhf6iuhbmsZX0us75FIiqJ7c06Mxw==",
"params"=>{"product_id"=>"1"},
"quantity"=>"1",
"event_id"=>"5",
"commit"=>"GO!",
"method"=>"post"}

You're sending a key params inside of the real params, to access your value do it like:
params[:params][:product_id]
or use:
collection_select(:object, :product_id, Product.all, :id, :name, prompt: true)
to get product_id in params

Related

Rails grouped_collection_select value_method with parameters

Is there any way to use grouped_collection_select value_method with parameters in Rails?
I'm trying to filter out a couple of options based on the current_user (effectively using something like: signed_by(current_user)) but I'm having some difficulty passing it in.
<%= f.grouped_collection_select :author_id, Author.posts, :signed_by, :title, :id, :email %>
Any ideas?
It's not possible to pass parameters to the value_method in this case because the value_method option is "the name of a method which, when called on a child object of a member of collection, returns a value to be used as the contents of its tag". In other words, it's just a string or symbol that will get called on the Author.posts collection.
If you're trying to filter out some options, I would suggest filtering them from the collection, and then passing the filtered collection to this method. Something like:
# author_helper.rb
def filtered_author_posts
Author.posts.where.not(signed_by: current_user)
end
<%= f.grouped_collection_select :author_id, filtered_author_posts, :signed_by, :title, :id, :email %>

Unpermitted params customer_ids

Super simple, dumb thing, which I can't figure out for more, than an hour now:
def user_params
params.require(:user).permit(customer_ids: []) # pg array column
end
My form:
= f.select :customer_ids,
options_from_collection_for_select(customers, 'id', 'name', user.customer_ids),
{ include_blank: 'Select customer', multiple: true, size: 15 },
class: 'form-control'
And while updating user I'm getting
Unpermitted parameter: customer_ids
How the heck in the world is that possible?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oCkUEi2pNajM0ydHUH2w6iYIq5eKjfCY5ig9U2qDTXxMqECCgQ2Dn9YtqkMqXlTmLl5q/OO8x23o/P50SnmgUg==", "user"=>{"customer_ids"=>"84"}, "commit"=>"Assign selected customer to user", "id"=>"2"}
Your form isn't sending in the customer_ids parameters as an array.
"user"=>{"customer_ids"=>"84"}
This is why. It should be (notice the square brackets):
"user"=>{"customer_ids"=>"[84]"}
If you declare the param as an array, it should be posted as an array. This is likely an issue in your form.
Usually, I would use checkboxes for something like this, but this depends on your user interface. Here's something similar I have done in the past.
= f.collection_check_boxes :customers, customers, :id, :name do |cb|
= cb.label
span.pull-right = cb.check_box
Look at the collection form helpers in Rails. A multiselect should work, but I have not used one this way.
Try changing your select tag like this
= f.select(:customer_ids, customers.collect { |c| [ c.name, c.id ] },
{ prompt: "Select Customer"},
{ multiple: true, size: 5, class: 'form-control' })

Rails 4 Checkboxes: Not getting saved to database

I'm using Rails 4 and I have 3 models: property, category, and categorization.
The application is here: https://github.com/ornerymoose/PropertiesAndCategories
The idea: you can update a property and there's 6 categories (6 checkboxes) to choose from.
I came across this SO post and it was helpful but alas, my values still aren't getting saved.
When I click the update button to update a property: the server logs (if I select checkboxes 3 4 and 5) will be:
**Processing by PropertiesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dcgy+3qBF4JLeu5MMATyqOc+jWyILXTfgURbqn+Ez8E2ru3rpzgPBqKDPWsy/QelQuQgOczdC8xDsOnnwSDAnQ==", "property"=>{"name"=>"Vizcaya", "category_id"=>["3", "4", "5", ""]}, "commit"=>"Update Property", "id"=>"6"}**
If I fire up rails console and check out the ^above property, category_id is set to nil. Why is this? My strong params are correct in the properties_controller I think:
def property_params
params.require(:property).permit(:name, :category_id => [])
end
Any and all input is greatly appreciated.
Why do you have ":category_id => []"? I'm pretty sure if you get rid of the "=> []" portion and just treat :category_id like :name (as I've shown below), it should work!
def property_params
params.require(:property).permit(:name, :category_id)
end
Ok, the first fix was here in the properties controller:
def property_params
params.require(:property).permit(:name, :category_ids => [])
end
I was initially using :category_id => [] which didn't return an error in the Rails server log, but wasn't saving the checkbox values to the database.
And then the second fix was fixing the collection_check_boxes arguments. Initially I had :category_id but I completely overlooked that it had to be :category_ids:
<%= collection_check_boxes(:property, :category_ids, Category.all, :id, :name)
Everything is working well now.

Rails - binding versus not binding a rails collection select

So I was experiencing an error when I was attaching a collection_select to my form_for object like so:
<%= f.collection_select(:city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
and getting the error:
undefined local variable 'city_id'
But when I don't bind the select like so:
<%= collection_select(nil, :city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
It works fine.
I just want to understand the theory behind why one works and the other doesn't?
I think what's tripping you up, primarily, is the concepts you have of what's going on here.
Nothing is “binding” anything to anything by calling a method on a form helper object. There are form helper methods, like collection_select, that can be used to build HTML elements. There are form builders that have methods, like collection_select that build HTML form elements for a form tied to an object.
The issue you're having here is that the FormOptionsHelper#collection_select method and the FormBuilder#collection_select method are not the same method and do not accept the same arguments:
FormOptionsHelper#collection_select vs FormBuilder#collection_select
Pay particular attention to the arguments provided. It's also worth noticing that FormBuilder essentially delegates this work to the template (i.e. FormOptionsHelper) and adjusts the arguments as needed.

need to add default value in f.select field to existing ones - rails 3.2

With the code I have below in the select field I have all the public_campaigns:
<%= f.select :campaign_id, #public_campaigns.map{|x| [x.name,x.id]} %>
public_campaigns is defined in controller with:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
In the form I select the campaign and fill up the rest of the form and at the submit action an invitation is created with campaign_id taken from the campaign I selected in the form, it can be anything from 1 to n
What I need now is to have a default item in select field that will have the value of 0 and named "No campaign", it means I invite someone to a campaign that I have not created yet and in invitation the campaign_id field will be 0.
Thank you for your time.
Do you really need 0? I think use of {:include_blank => "No campaign"} should be enough?
Try this:
<%= f.select :campaign_id, (#public_campaigns.map{|x| [x.name,x.id]} << ["No campaign",0]), {:selected => 0} %>
Well, the fastest way you can do this is something like this:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
no_campaign = Campaign.new(:id => '0', :name => 'No Campaign')
#public_campaigns.unshift(no_campaign)
I'm not sure why you are unable to do it this way:
<%= f.collection_select :campaign_id, #public_campaigns, :id, :name, prompt: 'No campaign' %>
Just check if campaign_id.nil? instead of assigning any value to campaign_id

Resources