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 %>
Related
I have a rails form which has intermediate_points field that corresponds to a text column in rails.
In model.rb
serialize :intermediate_points
In controller.rb
def set_params
params.require(:modela).permit(:intermediate_points)
end
In view.html.erb
<div class="field">
<%= f.label :intermediate_points %>
<%= f.select :intermediate_points, #intermediate_points, {}, multiple: true %>
</div>
The option appears as a multi select field. But I am not able to select multiple options in the view. How can I make the view to select multiple options?
I can't see any issue in you view code.
What I would like to know is what is the output of # intermediate_points
As it is a multiselect, therefore, you have to permit your params correctly.
In your controller, use below syntax to permit an array
def set_params
params.require(:modela).permit(intermediate_points: [])
end
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
The Attraction model belongs to Destination.
I'm trying to create a new attraction (text_field :name) but also linking it to an already-created destination. These destinations are rendered in a drop-down menu with this collection_select tag. By clicking submit, I'm want the attraction to be created and saved in the activerecord database with the Destinations foreign key.
f.collection_select(:attraction, :destination_id, Destination.all, :id, :name) %>
The whole block looks like this at the moment:
<h1>New attraction</h1>
Select a City
<%= f.collection_select(:attraction, :destination_id, Destination.all, :id, :name) %>
<div class ="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
How can I save the attraction to the database with the appropriate destinaton? Thanks in advance!!
I haven't use f.collection_select() so not sure what it is called inside the params, but lets assume for the following code that it's params[:attraction][:destination_id]. You can change your create action to:
def create
#destination = Destination.find(params[:attraction][:destination_id])
#attraction = #destination.attractions.build(params[:attraction])
if #attraction.save
...
else
...
end
end
That's assuming that you've created a has_many and belongs_to association.
If you run into a mass-assignment error then change the first line to:
#destination = Destination.find(params[:attraction].delete(:destination_id))
# this will return the deleted value, and in the next line of code
# you'll have the rest of the params still available
I post a "select" parameter via a form (and other fields)
My Model needs an ARRAY of the parent_musicstyle parameter, but gets an string.
Where and how do I convert this?
In the View, Controller or Model
Form:
<div class="field">
<%= f.label :parent_musicstyle %>
<%= f.select :parent_musicstyle,
Musicstyle.all.map { |m| [m.musicstyle, m._id] },
{:include_blank => "Select a parent (if needed)", :index => nil } %>
</div>
How can I extract/convert the specific param and convert it to an array?
Let's call your model musicstyle, and you'd have:
<%= form_for :musicstyle do |f|
...
<% end %>
Then, in your controller, you can do
pm = params[musictyle][parent_musicstyle]
pm = [pm]
That said, you'd have an array of one element, which is not useful. Is your select a multiple selection one?
I have something basic generated from nifty_scaffold in a partial form: _form.html.erb
<p>
<%= f.label :group_id %><br />
<%= f.text_field :group_id %>
</p>
Instead of a text field I want to convert the above from text_field to a drop down list which will be populated with groups which I set below.
My new action in Employee controller looks like this:
def new
#employee = Employee.new
#groups = Group.all
end
How do I make a drop down list where it will be populated with all groups in #groups variable
Additionally, how will edit action work? there I will want the assigned group to be preselected. Since I am using a partial form, same form will be used in edit as well.
<%= select("employee", "group_id", Group.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })%>
works!