I don't get the point how can I do that code with an select helper?
<select name="cube_name">
<% #cube_names.each do |cube| %>
<option value="<%= cube %>" <% if #cube_name == cube %> selected="selected"<% end %>><%= cube %></option>
<% end %>
</select>
I have a list (#cube_names) and want a HTML select box width all values of the list and the default value (param #cube_name) should be selected.
thanks.
The select_tag helper will not auto-set the selected attribute on an item you pass. It just builds the tag. Use something like:
<%= select_tag("id_of_my_tag", #cube_names.map { |cn| "<option#{cn == cube ? " selected='selected'" : ""}>#{cn}</option>" }.join("")) %>
The first parameter is the id of the select tag, the second is a list of options (here built by mapping the cube names to strings, then joining the array into a single string).
You could alternatively use the options_for_select to build the string:
<%= select_tag("id_of_my_tag", options_for_select(#cube_names, cube)) %>
Related
I need to add options to a HTML-selector combobox inside an ERB template. First I create an array named var_arr with <option value="foo">Foo</option>-items out of an array with items/values like foo and then I try to write it between the <select></select>-tags with each
<% var_arr.each do |option| %>
<%= option %>
<% end %>
What it generates is
<option value="article">Article</option> <option value="link">Link</option> <option value="photo">Photo</option> <option value="treenode">TreeNode</option>
But it's not treated as HTML. The options aren't displayed as options in the dropdown menu.
Where's the hidden caveat here?
Yours
von Spotz
ERB treats the text from variables as text and not as html you need to mark the options as html_safe to treat it as html like this.
<% var_arr.each do |option| %>
<%= option.html_safe %>
<% end %>
i have some records like this one
"boss/supervisor/employee"
"boss"
"boss/supervisor"
i would like to put all this records in a select input without repeating themselves... here is what i've done already
in my index i did this:
#jerarquy = Jerarquy.uniq.pluck(:name)
so when i get all the records from the column jerarquy:
<select>
<% #jerarquy.each do |jer| %>
<% jer.split('/').each do |j| %>
<option><%= j %></option>
<% end %>
<% end %>
</select>
i do get the data from the database but i think i have to change something in my controller in order to get the information i want in the order i want, for example if i select a boss i want to get /supervisor and /employee too but if i select a employee i want to only get a employee
This is how you would use flatten to convert the fields to a single array and then use .uniq
ary = ["boss/supervisor/employee", "boss", "boss/supervisor"]
ary.map { |string| string.split('/') }.flatten.uniq
=> ["boss", "supervisor", "employee"]
#jerarquy = Jerarquy.uniq.pluck(:name).map { |string| string.split('/') }.flatten.uniq
<select>
<% #jerarquy.each do |jer| %>
<option><%= j %></option>
<% end %>
</select>
However I would say that this code indicates a poor underlying design.
You should be letting the database handle pulling out unique records.
I have the following code which displays a dropdown menu (I have it this way the so the form shows the last selected option). I would like to pass the value associated with the selection to a method in my controller.
I want to save this to the the value of "choice" which is specified as an integer in my user model. The initial value of choice is 0.
I can't seem to find a way to do this with the local "value".
<select class="form-control">
<option value= 1>One</option>
<option value= 2>Two</option>
<option value= 3>Three</option>
</select>
<%= link_to "Select", {controller: :controller_name, action: :method_name, value: :value} %>
Controller method (I am using Devise to call "current_user")
def method_name
current_user.choice = value
current_user.save
redirect_to root_path
end
Try a form, here is an example of what you need below:
<%= form_tag(url_for(controller: 'controller_name', action: 'method_name'), method: :method_type(PUT, POST, etc.) do %>
<div class="form-group">
<%= label_tag(:choice, "Choice") %>
<%= select_tag(:choice, options_for_select([0, 1, 2, 3], selected: 0)) %>
</div>
<%= submit_tag 'Save'%>
<% end %>
As a commenter eluded to, in your method you can then use:
def method_name
current_user.choice = params[:choice]
current_user.save
redirect_to root_path
end
I have a rails 4 app that has a form that looks like:
<%= form_for #store, :html => {:multipart => true} do |f| %>
<%= f.fields_for :products do |product| %>
<%= render partial: "edit_product_fields", locals: {product:product} %>
<% end %>
<%= f.submit %>
<% end %>
and the edit_product_fields partial looks like:
<select>
<option value="Textbook" <% if product.type_of == "Textbook" %>selected<% end %>>Textbook</option>
<option value="Magazine" <% if product.type_of == "Magazine" %>selected<% end %>>Magazine</option>
<option value="Book" <% if product.type_of == "Book" %>selected<% end %>>Book</option>
</select>
When I have the code like this, I get the error:
undefined method `type_of' for #<NestedForm::Builder:0x00000102304f78>
but if I change the render line to (I just made it less explicit by taking out partial:):
<%= render "edit_product_fields", locals: {product:product} %>
I get the error:
undefined local variable or method `product' for #<#<Class:0x0000010235a248>:0x0000010684b3c0>
In your first code block, you have a Builder object being stored as product.
Fortunately, Builder provides an object method so you can access the actual object that it's representing in the form:
<select>
<option value="Textbook" <%= 'selected' if product.object.type_of == "Textbook" %>>Textbook</option>
<option value="Magazine" <%= 'selected' if product.object.type_of == "Magazine" %>>Magazine</option>
<option value="Book" <%= 'selected' if product.object.type_of == "Book" %>>Book</option>
</select>
Well type_of is not a ruby method. It looks like it's an attempt to type check though... But type checking is generally frowned upon in duck-typed languages. At any rate, we can take advantage of a Rails Form Helper instead and make this code a one-liner. But first...
The reason the render is complaining about undefined local variable or methodproduct'` after your change is because the render call expects either the first argument to be a partial name and the second to be a hash of locals:
<%= render "partial_name", local1: "a", local2: "b" %>
... or it expects the the whole thing to be a hash:
<%= render partial: "partial_name", locals: { local1: "a", local2: "b" } %>
Now, back to the form helper.
What you're looking for here is the select helper. This makes the following possible:
<%= product.select :type_of, %w[Textbook Magazine Book] %>
And this will render all of the same html as you've put into your _edit_product_field.html.erb partial. So you should probably just get rid of the partial to save on rendering time and put this directly into your form.
One other, minor point. It may be less confusing to call the fields_for block variable product_form instead of product. This is to help keep in mind that this is not a Product object you're dealing with, but rather a Form Generator object. Also, you don't need to specify :multipart => true in Rails 4 forms -- Rails knows to make the form multi-part if you have a file upload element within the form body.
So putting it all together:
<%= form_for #store do |f| %>
<%= f.fields_for :products do |product_form| %>
<%= product_form.select :product, %w[Textbook Magazine Book] %>
<% end %>
<%= f.submit %>
<% end %>
I am using Perl (and Dancer) with HTML to create a form. On my form I have some fields like so:
<input type="text" name="keywords" id="keywords" value="<% keywords | html %>"/>
<select multiple="multiple" id="sentences" name="sentences" size="3"/>
<% IF list_of_sentences %>
<% FOREACH sentence IN list_of_sentences %>
<option value=<% sentence %>/>
<% END %>
<% END %>
</select>
These are, of course, inside their own divisions and there is more code around, but these are the essentials of what I've got.
These are in main.tt which is interacted with using myApp.pm and there are parameters which are passed back and forth (e.g. keywords and list_of_sentences here). The code is set up with two divs, one with the form and the other purely to show the output. When the form is submitted the output shows correctly, but the select field selections are not retained.
So therefore, what I want to do is make it so that the chosen selection(s) from the 'multiple select' are kept selected.
As an example, in my 'input' keywords field I have the value retained because it has been passed back from the .pm file using param("keywords") using template 'main' .... This works with value="<% keywords | html %>" in the html code.
I have tried to do the same for the multiple select using param("sentences"), but this returns a blank field.
To hazard a guess at the problem it is because there is more than one option and maybe because I am getting the options from the .pm file to start with, but I haven't found the solution yet, so any help would be welcome.
You want to use the selected="selected" attribute on the option element. I would suggest passing both the name of the sentence and flag stating if the option should be in the selected state. For example:
<% IF list_of_sentences %>
<% FOREACH sentence IN list_of_sentences %>
<option value="<% sentence.value %>"<% IF sentence.is_selected %> selected="selected"<% END %> />
<% END %>
<% END %>
And use the following data structure for your sentences:
{ value => $value, is_selected => 0|1 }