How I can to pass an array like generated by a controler with the following code:
tipos= Type.all
#listadotipos=[]
tipos.each do |h|
#listadotipos.push(h.name)
end
The last code generate an array called #listadotipos. This array is passed to a html.erb view and I want to show all array components inside a Select field in the view.
The select field works this:
<%= f.select :make, options_for_select(["option1", "option2"]) %>
How I can do this. Please help me.
First of all your controller code can be cleaned up like so:
#listadotipos = Type.all.map(&:name)
Your view code should work if you use the variable instead of your hard coded array:
<%= f.select :make, options_for_select(#listadotipos) %>
With the below code inside the controller :
#listadotipos = Type.all.map { |type| [ type.name, type.id ] }
You can write as below :
<%= f.select :make, #listadotipos %>
Read 3.2 Select Boxes for Dealing with Models guide.
Try this in view
<%= f.select :make, Type.all.collect(&:name) %>
For id
<%= f.select :make, Type.all.collect{|type| [type.name, type.id] } %>
This are the solution. And I dont need code into the controller:
<%= f.select :make, options_from_collection_for_select(Type.all, :id, :name) %>
Thanks all
I am facing this difficulty. This works for me :
<% cat_st = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 %>
<%= Categories.where(cat_id: [cat_st]).distinct.count(:af_id) %>
However, I need to create the cat_st string from my GET parameters. When I create it as a string, it gets only the first integer. Actually, I really don't know what the cat_st variable is. I tried to create an array, but it doesn't work. Any ideas?
( it doesn't work)
<% cat_st = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29' %>
<%= Categories.where(cat_id: [cat_st]).distinct.count(:af_id) %>
cat_st = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29'
cat_array = cat_st.split(',')
Categories.where(cat_id: cat_array).distinct.count(:af_id)
There may be a better solution for what you're trying to achieve if you give some additional details about what you're trying to do, seems like it should be some sort of search form that passes multiple ids. Something like this for example:
class Search
include ActiveModel::Model
attr_accessor :category_ids
end
<%= form_for Search.new, url: 'whatever_the_url_is' do |f| %>
<div class="field">
<%= f.label :category_ids %>
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name %>
</div>
<% end %>
search = Search.new(search_params)
Categories.where(cat_id: search.category_ids).distinct.count(:af_id)
You can pass just a range to the where method so:
Categories.where(cat_id: (1..29)).distinct.count(:af_id)
and to pass data via GET request just use a limitation:
http://host.domain/controller/action?begin=1&end=29
controller:
def action
#cat = Categories.where(cat_id: (params[:begin].to_i..params[:end].to_i)).distinct.count(:af_id)
end
then render the view using #cat variable.
If you add square brackets to the param name in the URL query, Rails (specfically Rack) will interpret that as an array:
http://some.route.url/path?foo[]=bar&foo[]=buzz
The controller will read params[:foo] as ['bar', buzz']
However, I have a feeling that use case problem you are attempting to solve might have a much simpler solution. Care to go into any more detail?
how can we define id for this rails select statement , i have tried doing in this way like
<%= f.select :state, options_for_select(Contact::STATES), :id=>"state_job" %>
but it is not showing any id when i inspect it in the browser. Please help me out
<%= f.select :state, options_for_select(Contact::STATES) %>
The select tag helper looks for options, then html_options, you just need to make sure your id is in the right place (html_options) by passing something to the options parameter:
<%= f.select :state, options_for_select(Contact::STATES), {}, {:id=>"state_job"} %>
Here is the problem I'm having, and I have tried thinking around, but still can't figure out the solution. So I have two models
User
Data
and Experience belongs to user, and accepts nested attributes
Now here comes the problem ! I have page/form where I would like to update or insert.
so in the Data model
flavor, body
So How do I add form tag where I can specify my flavor but let user decide the body so for example, currently I have
<%= f.text_field :body, placeholder: "...." %>
So how do I do something like (wrong syntax)
<%= f.text_field :body, :flavor => "someflav" , placeholder: "...." %>
<%= f.text_field :body, :flavor => "Otherflav" , placeholder: "...." %>
and so on...
How does one achieve this ? I have looked around rails api, and but couldn't figure out how to achieve my issue.
Thanks for your consideration and time.
You need to use fields_for
Rails constructs input names that help it determine exactly what attribute goes where.
For instance:
user[datas_attributes][0][body]
Since (if I am interpreting you correctly) User has many Datas, it would look something like this:
<%= fields_for :datas do |data_fields| %>
<%= data_fields.text_field :body %>
<% end %>
There are a few things you need to do to make this work.
In your model, you need to add the following two lines:
accepts_nested_attributes_for :datas
attr_accessible :datas_attributes
Trying to format a Datetime select helper by passing in a class like so has not been working. I at first thought Bootstrap might be wrong, but after looking at the source, 'span2' is not there.
<%= f.datetime_select :arrival_nor_tendered, class: "span2" %>
This seems like the headslap way to do it but I may be wrong.
Try this:
<%= f.datetime_select :arrival_nor_tendered, {}, { class: "span2" } %>
The last parameter is html_options but it has a hash parameter before it and ruby may missinterpret the two.