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.
Related
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 %>
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
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 a many-to-many relationship between Notes and Stacks. I am creating a form where a user can create a new note, with a title, body, and then a group of check boxes. The group of checkboxes are various Stacks. I want a user to be able to associate one or multiple Stacks with a particular note.
I am using the simple_form gem to do this. Everything is working except the Stacks will not save to the database. The title and body save, but not the Stacks. I have tested both sides of the relationship manually in the console, and they do yield the expected results -- it works.
One thing I notice when I watch what is going on in the Rails Server tab is Unpermitted parameters: stack_ids, as seen here:
Started POST "/notes" for 127.0.0.1 at 2014-10-06 16:00:21 -0600
Processing by NotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gMpbaYio0qkPLLrR6ICG0IJ7XNoy3Rn4RHLm3vwUU+I=", "note"=>{"title"=>"asdfs ", "body"=>"asdfasdf asfsadf ", "stack_ids"=>["1", "2", ""]}, "commit"=>"Create Note"}
Unpermitted parameters: stack_ids
My notes_controller.rb has these params:
def notes_params
params.require(:note).permit(:title, :body, :stack_id)
end
My stacks_controller.rb has these params:
def stacks_params
params.require(:stack).permit(:title, :description, :note_id)
end
Here is the form:
<%= simple_form_for #note, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :title, placeholder: ":title", id: "note-form-title-field", class: "note-form-fields" %>
<%= f.input :body, placeholder: ":body", id: "note-form-body-field", class: "note-form-fields" %>
<%= f.association :stacks, as: :check_boxes %>
<%= f.button :submit %>
<% end %>
I have tried replacing :stack_id in the params with other variations, and always get the same result. Any ideas on how to get these Stacks to save to the database, associated with a note? Thanks.
This StackOverflow posting provides a detailed solution.
In summary, when declaring strong parameters, I needed to explicitly map the stack_ids key to an empty array in my notes_controller.rb file:
def notes_params
params.require(:note).permit(:title, :body, stack_ids: [])
end
I have a question about forms. I have a fairly standard form that saves a post (called an eReport in my app) with a title and body. The table also has a "published" field, which is boolean. The saved eReport only shows on the public site if this field is set to true, with false being the default.
Rather than the default check box, I would like to display two buttons at the end of the form: a "Publish Now" button and a "Save as Draft" button. If the user presses the former, the published field would be set to true. If the latter, then false. In PHP, I used to display 2 submit fields with different name values, then handle the input with an if/else statement to determine the proper SQL query to build. In Rails, I'm assuming I would place this logic in the controller, under the appropriate action, but I'm not sure how to manipulate the name or id values of buttons.
For the record, I'm using Formtastic, but if someone could show me how to do this with the default Rails form tags, that's OK too. Here's the code for my form as it stands right now:
<% semantic_form_for #ereport do |form| %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
<% form.buttons do %>
<%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
<% end %>
<% end %>
Thanks in advance for the help!
I don't know about formtastic, but with the default rails form builder, you could do it like this:
<%= form.submit "Save with option A", :name => "save_option_a" %>
<%= form.submit "Save with option B", :name => "save_option_b" %>
Then in the controller, you can pick those up in params:
if params[:save_option_a]
# do stuff
end
in addition to #iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)
Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}
where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.
which was obtained by submit_tag "Confirmar", :name => 'comentar'
So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...
and retrieve them in your controller
if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end
I think you need to use jQuery.
You can bind the button click event and submit the form for specified location.