I have a form which I'm dynamically adding textareas. The form comes this way:
<%= semantic_form_for :requisito, :url => update_requisitos_tramites_path, :html =>{ :id => "form_edit_req" } do |f| %>
...
<% end %>
When some button is clicked, some function is triggered and adds inside the form the next code:
<textarea rows="3" class="textarea-obs" name="requisito[observacion]">¿Por qué?</textarea>
and it gets rendered as expected (and the code is in the right place, I checked with the chrome's js console.)
I understand that the input type :text in a form gets converted to a textarea with some id and class but also with a name like model[attribute] for getting the value from the params in the controller like:
params[:model][:attribute]
or in my case:
params[:requisito][:observacion]
but I'm printing the params in the server's log and not value is stored with those symbols. Any ideas?
I didn't solved the recognition problem, just changed the code, so I wrote the textarea in the view but hidden. The javascript was for showing and hiding it, and if you write the name the way I said, it works perfectly! I think was the fact that I was adding the text area dynamically.
Related
Is there any method to use beneath form_for that creates a div or span tag that will be inserted into the database? Something like:
<%= form_for #object do |f| %>
<%= f.div_field :column %>
<% end %>
The reason for this is I have several javascript functions that update the content within the div tag and I want the content to get stored in a table in my database. Is this possible, or do I need to just use text inputs with uneditable content?
Don't know about that, but you could always use f.hidden_field :column. In your javascript, when you update the content of the div, also update the content of the hidden field. That way, you can still submit your form and get the content to the server without having to expose a text field.
Perhaps you could also style the immutable text area such that it looks less like a text area; hide the scrollbars and borders and it may look just like a div. But if you want a real div, I would go the hidden field route.
In my form, I used the span tag like the following:
<%= content_tag :span, f.object.User, class: 'username' %>
It looks like the following in HTML after i selected the value:
<span class="user" style="">Antony</span>
The problem is id doesn't get the value to the database when we create a form. I don't know the exact problem is. I want to use this content tag instead of text_field to get the value.
Thanks.
When you submit an HTML form, the only values that get POSTed are those that are in input fields such as text fields, selects, checkboxes, buttons, etc. Content that is simply on the page -- in a span or not -- will not get posted back to the server. That isn't a Rails issue, it's just the way HTML works.
I'm not exactly sure what you're trying to do here, but a common approach when you want to display a value (not in an input box) and also post the value back with the form, is to both render the value on the page (in a span or however you want) and also add a hidden input field (hidden_field_tag) that also has the value in it.
Yeah, Jacob is correct. Better create a hidden field
<%= f.hidden_field :user, class: 'user' %>
<%= content_tag :span, f.object.User, class: 'username' %>
The first line get the value in it. I hope, Jacob answer would help you. :)
Problem solved. HTML5 localStorage messed with me.
I'm trying to populate a form with parameters from the new()-method, and I can't get it to work.
Every user has default values for the form saved in the database(in a table called defaults), and when you create a new record I want it to be populated with the values from that table.
#default = Default.find_by_user_id(current_user.id)
#invoice = Invoice.new(:title => #default.title, :company_information => #default.company_information)
render 'create'
and then in my view:
form_for #invoice, :url => { :action => "create"} do |f| ...
What happens is that the values that are default for invoice are created, but not the ones created in the new()-method.
The weirdest part is that when I check the source code after the page is loaded, the inputs value attributes is filled with the correct information, but not rendered on the page...
What you're doing here:
Invoice.new(:title => #default.title, :company_information => #default.company_information)
Makes sense and should work…unless those fields are protected from mass assignment.
class Invoice << ActiveRecord::Base
attr_accessible :some, :other, :fields
...
end
This would allow you to set :some, :other, (and) :fields when you initialize your Invoice object, but it will prevent you from setting any other "attributes".
Strange, I don't see anything wrong with what you are trying to do... maybe something on the browser side (javascript, css, etc) is fowling things up?
Check to see if there is something selectable inside the form inputs or try creating a vanilla form without any javascript or css. Or, you might even try simply printing the contents of the attribute in the html (without using input/textarea tags) using something like:
<%= #invoice.title %>
This will at least help confirm that the default values where indeed set. Additionally, using:
<%= f.object.title %> # place me inside the form_for block
will help you confirm that the form builder instance also has the correct value.
Good luck.
my controller uses code like this:
if params[:commit] == "Submit"
this used to work fine when I just had buttons. however, now I am using images as buttons like below:
<%= image_submit_tag 'butons/Add-08.png', :class => 'image-button-submit' %>
How can I pass the commit variable with value Submit along with this image_submit_tag?
KandadaBoggu is right, but that will (in most browsers) give you a params['commit.x'] and params['commit.y'] rather than just params['commit'].
If there's only the one button on the form (and you're posting to the same action that renders the form) you can do if request.post? instead, but that's only going to work if there's just one button, ie: only submit, not submit and cancel.
As per the image_submit_tag documentation you can pass any HTML options. I haven't tested this but the following code should work.
<%= image_submit_tag 'butons/Add-08.png', :name =>"commit", :value =>"Submit" %>
I would try passing it as a hidden field inside of the form you're submitting.
hidden_field_tag "commit", "Submit"
I want to have a text box that the user can type in that shows an Ajax-populated list of my model's names, and then when the user selects one I want the HTML to save the model's ID, and use that when the form is submitted.
I've been poking at the auto_complete plugin that got excised in Rails 2, but it seems to have no inkling that this might be useful. There's a Railscast episode that covers using that plugin, but it doesn't touch on this topic. The comments point out that it could be an issue, and point to model_auto_completer as a possible solution, which seems to work if the viewed items are simple strings, but the inserted text includes lots of junk spaces if (as I would like to do) you include a picture into the list items, despite what the documentation says.
I could probably hack model_auto_completer into shape, and I may still end up doing so, but I am eager to find out if there are better options out there.
I rolled my own. The process is a little convoluted, but...
I just made a text_field on the form with an observer. When you start typing into the text field, the observer sends the search string and the controller returns a list of objects (maximum of 10).
The objects are then sent to render via a partial which fills out the dynamic autocomplete search results. The partial actually populates link_to_remote lines that post back to the controller again. The link_to_remote sends the id of the user selection and then some RJS cleans up the search, fills in the name in the text field, and then places the selected id into a hidden form field.
Phew... I couldn't find a plugin to do this at the time, so I rolled my own, I hope all that makes sense.
I've got a hackneyed fix for the junk spaces from the image. I added a :after_update_element => "trimSelectedItem" to the options hash of the model_auto_completer (that's the first hash of the three given). My trimSelectedItem then finds the appropriate sub-element and uses the contents of that for the element value:
function trimSelectedItem(element, value, hiddenField, modelID) {
var span = value.down('span.display-text')
console.log(span)
var text = span.innerText || span.textContent
console.log(text)
element.value = text
}
However, this then runs afoul of the :allow_free_text option, which by default changes the text back as soon as the text box loses focus if the text inside is not a "valid" item from the list. So I had to turn that off, too, by passing :allow_free_text => true into the options hash (again, the first hash). I'd really rather it remained on, though.
So my current call to create the autocompleter is:
<%= model_auto_completer(
"line_items_info[][name]", "",
"line_items_info[][id]", "",
{:url => formatted_products_path(:js),
:after_update_element => "trimSelectedItem",
:allow_free_text => true},
{:class => 'product-selector'},
{:method => 'GET', :param_name => 'q'}) %>
And the products/index.js.erb is:
<ul class='products'>
<%- for product in #products -%>
<li id="<%= dom_id(product) %>">
<%= image_tag image_product_path(product), :alt => "" %>
<span class='display-text'><%=h product.name %></span>
</li>
<%- end -%>
</ul>