I'd like to populate the value of a textarea input with a csv string. Here's my csv string (it's in a rails view at this variable #item[:template]):
x,y,series,size
2,-0.083014839,Group 0,0.883928284
-9,0.355697349,Group 0,0.149154477
5,-0.256459661,Group 0,0.066308001
3,-0.243723214,Group 0,0.388138931
7,-0.663022927,Group 0,0.09761712
0,-0.587616252,Group 0,0.246573359
Here's my textarea tag in rails:
<%= text_area_tag 'template', nil, class: 'form-control temp-grab',
value: #item[:template] %>
The textarea isn't actually populating though. I don't know if it's because it's a multiline string or because the characters aren't escaped. I've tried simple_format(#item[:template]) but it isn't working. Any ideas?
Per the documentation, any parameter other than size, rows, cols, disabled, and escape becomes HTML attributes. HTML textareas do not have a value attribute. You instead want to do this:
<%= text_area_tag 'template', #item[:template], class: 'form-control temp-grab' %>
Related
I search on the web, and it seems we can do it by apply a style: "background-color:colorName;" on the desired option.
But after many researches and tries, I cannot find how to do this with the rails form helpers.
This is what I have now :
<% users_array = ["Choose your player"] %>
<% users_array += User.all.map { |user| [user.name, user.id ]} %>
<%= select("player-#{user.id}-#{category[0]}", nil, options_for_select(users_array, user.name.to_s ), { }, { class: "selectstyle col-md-2" }) %>
I display all my model content with a default selected option specified in the options_for_select, but I want to add a different color to each users of my model displayed in this select.
The only example I found on the web are for a select without model displaying, and all my tries failed. How can I do ?
Check here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
You can optionally provide HTML attributes as the last element of the array.
You can do something like:
users_array += User.all.map do |user|
html_attributes = {}
html_attributes['style'] = '...' if <some_condition>
[user.name, user.id, html_attributes]
end
I am working on a solution to add custom data-attributes to option-tags with the collection_select form helper in Rails.
I studied some posts at stackoverflow and did a lot of trial and error after consulting some API documentation. I am nearly there but sadly my solution only adds attributes to the select-tag not to the option-tags.
This way I am populating the html-options-hash (6th position):
<%= f.collection_select(:parallax_id, #parallax.all, :id, :title, {}, { :"data-icon" => #parallax.map{ |p| "#{p.image}"}} ) %>
This results in a select tag like:
<select data-icon="/uploads/image/image/4/169_strecken-ausgang.jpg" name="game[parallax_id]" id="game_parallax_id">...</select>
But I want the option to get the data-icon attribute. When I switch positions and add my data-icon to the options-hash (5th position) nothing is output.
Is this what you want?
= f.select :parallax_id, options_for_select(#parallax.map {|p| [p.title, p.id, {'data-icon' => p.image }]})
Just FYI I was looking at this matter, found the more fitting solution here: How do I set the HTML options for collection_select in Rails?.
I'm using rails form. In that form, I've a text_area. I want to show pre-filled data in text area of rails. The data is html content.
I tried following,
<% test_val= "<h1>Test Value</h1>" %>
<%= form.text_area(:myval, :value => test_val.html_safe, size: '30x10', :style => 'border: 10;') %>
myval is my form variable. But instead of printing Test Value in bold and bigger font in text area, t showed <h1>Test Value</h1>. I want the content in HTML tag to be printed with HTML formatting. Is it possible? If yes, how do I achieve it?
p.s. I'm using ruby v1.9.3 & rails 3.1.0
This is not possible in a textarea, this is not related to rails but to Html.
Instead you can use a div and set it with contenteditable =true
example:
<div contentEditable="true">content goes here </div>
If you are using javascript you can store the result in a variable like so:
var myvar = document.getElementById("divid").innerHTML
You may want to use a wysiwyg editor.
I suggest you to have a look at
https://github.com/Nerian/bootstrap-wysihtml5-rails
With simple_form you can simple use it as:
<%= f.input :content, as: :wysihtml5 %>
As I said previously, you should probably use a contentEditable unfortunately, you won't be able to use the rails existing helper to send the form, and have to use a little Javascript to build the form, I do not find the method really 'clean' but it works:
<html>
<head></head>
<body>
<form id="my_form" action="#" method="POST">
<div id="editable_div" contenteditable>
<h1>Editable HTML Content</h1>
</div>
<input type="submit" />
</form>
</body>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
var form = $("#my_form");
form.submit(function() {
console.log('submitted');
form.append($('<input/>', {
type: 'hidden',
name: 'html_content',
value: $("#editable_div").html()
}));
});
</script>
</html>
You should be able to adapt this code to your rails one quite easily.
I am using Simple Form in a Rails 4 app for a #user object and have the line...
= f.input :entity_name
This generates the HTML name='user[entity_name]' inside the input tag. I would like to change it because of some custom processing I am doing in the controller but haven't found a way to do so.
I've tried changing the line to...
= f.input :entity_name, name: 'entity[name]'
...but this doesn't seem to affect the generated HTML at all. So far I haven't found anyone else with this question on Google/Stack Overflow.
Does anyone know if/how it's possible to change the name attribute through the Simple Form helper?
Thanks in advance!
The trick is using the input_html option.
= f.input :entity_name, input_html: { name: 'entity[name]' }
If you're looking to just change the shown label of the field:
= f.input :entity_name, label: 'new_input_field_name'
I have one input field and 2 buttons, each button performs a different function. i want one of the buttons to take the value of the input field and add it to a specified Url (as a param) that button should go to.
so if the value inserted in to the box is "dog" then after clicking the button the URL should be "/go_somwhere?value=dog"
can i do it in just html or do i need a ruby method?
i'm using rails.
thanks
<% form_for :item, :url=>{:action=>'go_somwhere'}, :html => { :method=>"get"} do|f| %>
<%= text_field_tag :value, "" %>
<input type="submit" value="Submit" >
<% end %>
Seems like you would need to do this via javascript on the client side . The button should have a function attached to its click event . This function should read the value from the input box and change the url ( window.locations ) .