I've got a form for a "group", and on it is a select box like so:
<%= f.text_field :description %>
<%= select_tag 'custom_people', People.all %>
in my group controller:
def group_params
params.require(:group).permit(:description, :custom_people)
and when the form submits, I can see custom_people in my params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"",
"group"=>{"description"=>"something"}, "custom_people"=>"1",
"commit"=>"Save"}
but in my controller action group_params only gets
{"description"=>"something"}
Is this because the select_tag has a custom name, and I need a different syntax in my strong params? How can I fix this?
You need to name the select_tag manually to fix the problem. With the below code, you now get the custom_people inside the groups: {..} params.
<%= select_tag 'group[custom_people]', People.all %>
Related
Right now I'm using a hidden field tag to get the plan_id. But when I use I binding.pry it's nested in the accounts hash. In order to get to the plan_id I have to do this params[:accounts][:plan_id] when I simply want to do this params[:plan_id]. How can I achieve this? Here is my code.
<label>
<%= f.hidden_field :plan_id, value: 1 %>
<%= f.submit "Select", class: "button-big reverse-blue" %>
</label>
Here is my params look like
{ "account"=>{"name"=>"something", "plan_id"=>"1"}, "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}
I want them to look like this
{"account"=>{"name"=>"something"}, "plan_id"=>"1", "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}
Again. I'd like to access the plan Id by simply typing this params[:plan_id]
You could use hidden_field_tag
<%= hidden_field_tag :plan_id, 1 %>
I'm using collection_select to select an option from a drop down. When I submit the form I want to send multiple params into the params hash. In this case 'team_id' and 'team_name'. team_id is showing up in the params hash just fine. 'team_name' is the value which shows up in the drop down list itself.
view.html.erb
<%= form_for #carpool do |f| %>
<h3>Select Team</h3>
<div class="form-group">
<%= f.collection_select :team_id, #ts_teams ? #ts_teams : [], :id, :name, include_blank: true %>
</div>
<%= f.submit 'Create Carpool', :class => 'button left' %>
<% end %>
params hash
{"utf8"=>"✓", "authenticity_token"=>"bdazhLNLZ0QunrpJT7Gu63ipX76WME+ENSxL/B0XGeFL/GP5nishozmQENe22aelfcnnhnPBr4B35MeRL+kJLQ==", "carpool"=>{"team_id"=>"1923565"}, "commit"=>"Create Carpool", "controller"=>"carpools", "action"=>"create"}
How can I pass team_name into the params hash?
You can get what you want without jumping through flaming hoops by just creating an instance of Team (which you are probably already doing anyhow) and just getting the name directly.
In your controller method:
#team = Team.find(params[:team_id])
#team_name = #team.name
Update through the drop down is not working. I have a form_tag inside the form tag I have the loop. So, the value will pass in an array. How can I update the value from it.
In my Period controller I have
def period
#period = Period.all
end
def period_update
Period.update(perio_params)
end
private
def perio_params
params.require([:period][:subject_id]).permit(:subject_id)
end
In the period_update views I have
<%= form_tag perio_update_institutes_path, method: :put do %>
<% #period.each do |p|%>
<%= p.subject.name %>
<%= select_tag('subject_id', options_for_select(Subject.all.collect{ |s| [s.name, s.id]}), {prompt: 'Select Sub'})%>
<%end%>
<%= submit_tag %>
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"6LJrh2ct7VMGU6Siq/RXIMGz4kkxkVN81Jqa+eRcKb3rXq3XzBlv8gjHvjjVsPsJ4LF7ZEHF/GQ8+0906lhyUg==",
"period"=>{"subject_id"=>["2",
"2"]},
"commit"=>"Save changes"}
how can I update the subject_id array.
The ERROR is
no implicit conversion of Symbol into Integer
I have the subject model, where all the subject will be in the collection.
The Period model has the subject_id. How can I update.
I am new to rails, and I am stuck with this. Thank you for advance.
Try if it behaves differently with options_from_collection_for_select:
<%= select_tag('subject_id', options_from_collection_for_select(Subject.all, :id, :name), {prompt: 'Select Sub'}) %>
I have one form in views of controller customer_tasks that uses a dropdown menu with column names from model customer.
In the form, that dropdown menu is called with the following:
<div class="field">
<%= f.label :search_column1 %><br>
<%= select_tag :search_column1, options_for_select(Customer.translated_searchable_columns, params[:search_column1]), :include_blank => true %>
</div>
In my model customer the dropdown is generated with:
def self.searchable_columns
wanted_columns = ['id', 'aa_code', 'name' ]
self.column_names.select{ |column| wanted_columns.include?(column) }
end
def self.translated_searchable_columns
columns = self.searchable_columns
result = columns.map{ |column [Customer.human_attribute_name(column.to_sym), column] }
result
end
In the customer_tasks controller I have the :search_column1 included on permitted parameters like:
def customer_task_params
params.require(:customer_task).permit(:task_name, :period, :client_type, :search_column1, search_column1: [])
end
But the output of the development.log is:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"H4waTsM4D1+EZX+deFozJut6FRS36sVQe7d7a1oRK2w=", "customer_task"=>{"task_name"=>"blah", "period"=>"", "client_type"=>"Γ"}, "search_column1"=>"aa_code", "commit"=>"Ενημέρωση Customer task", "locale"=>"el", "id"=>"4"}
The dropdown is working and sending the chosen option.
Why despite the fact that the parameter is permitted in the controller, it is not being saved/written to the database?
Why "search_column1"=>"aa_code" appears outside the parameters to the database?
What I am missing here?
You are using select_tag that's why "search_column1"=>"aa_code" appears outside the parameters to the database., if you use select it will go in the customer_task hash.
Change your select_tag to:
<%= f.select :search_column1, options_for_select(Customer.translated_searchable_columns, params[:search_column1]), :include_blank => true %>
I have simple form which allows users to filter search results according to location_id which is given in collection_select form's field. I want to render narrowed results if specific location is selected, and render all results if user selects default "Please select" field.
This code works good for separate locations, but not working if user selects "please select" field. params[:location_id].blank? in controller not working.
view
<%= form_tag #city %>
<%= collection_select 'location_id', nil, #city.locations, :id, :address, prompt: true %>
<%= submit_tag "sort", :name => nil, class: "btn btn-success travel-button", id: "hotels-radius-search-button" %>
<% end %>
controller
if params[:location_id].blank?
...render all results...
else
..render specific results...
end
Update
Server's response
Parameters when default "Please select" option is selected -- not working
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>[""], "_"=>"1365350127863", "id"=>"75"}
Parameters when user select any option in menu -- succesful
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>["811"], "_"=>"1365350127865", "id"=>"75"}
or
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>["808"], "_"=>"1365350127865", "id"=>"75"}
etc
Posting my answer, since comments cannot be accepted :-)
collection_select was used and was sending an Array.
Therefore usage of select_tag was suggested.