i have used observed_field for arrays of data... but i am having trouble making a simple, one field application of it work. i must be missing a very simple detail. have searched extensively for basic syntax and cannot find anything that applies. when i change the selection, the change is not saved and there is no error msg.
<% form_for :team, :url => update_item_leader_group_path do |f| %>
<%= f.select :item_id, #selection_collection %>
<span id="trigger_spinner" style="visibility: hidden;">
<img src="/images/spinner.gif" alt="Loading..." />
</span>
<%= observe_field 'group_item_id',
:url => { :controller => :group, :action => :update_item },
:method => :put,
:with => "'trig=' + $('group_item_id').value" %>
:loading => "$('trigger_spinner').setStyle({visibility: 'visible'});",
:complete => "$('trigger_spinner').setStyle({visibility: 'hidden'});" %>
<% end %>
The onchange event may not be observed until you leave the focus of the select field. To watch some field changes instantly your only option is to use a form observer.
BTW: The dom id of your field is probably not item_id due to usage of the form builder. #Swards already pointed that out.
The id of the field is usually something like id='group_item_id', could this be the issue?
corrected syntax above
1. form path
2. observer "url"
3. observer "with"
there was also a routing problem. i had to move update_item from a map.resources member to collection
Related
I'm trying to add an input to a form that already exists in one of the Spree admin views. Here's the override text (I cut out some cruft that was display centric and noisy):
new_html = "
<div id='neato_new_field'>
<p id='product_display_value'>
<label for='display_value'>Display Value</label>
<br>
<input type='text' name='product[display_value]' size='30' id='product_display_value' value=''>
</p>
</div>"
Deface::Override.new(:virtual_path => "spree/admin/products/_form",
:insert_after => "div.clearfix",
:text => new_html,
:name => "add_display_value")
What I need to know how to do is to set the value of the 'value' property on the input so that it picks up what is currently on that object when this view is rendered.
I tried putting value='#{#product.display_value}' in there but it complains that #product is nil. Can I even use interpolation like that to pick up the display_value? is this defacing done before the controller even tries to render or after?
I'm wondering now how to address this. Am I going to have to skim off the product id from the html that is already there, use it to load up the product and THEN put that value in there? I'm really hoping I'm missing something fundamental that makes this a trivial problem to solve :P That's kind of been the case with a lot of the Spree customizations--it's not clear how to do it at first but once I figure it out, it's easy as pie to implement my customizations...
thanks,
jd
The answer was fairly easy once I understood it.
Instead of using :text => to some random html markup, I pointed at a partial that as a result got all the neat behavior you get in any template view -- the variables in scope, in particular.
The end solution looks like this:
Deface::Override.new(:virtual_path => "spree/admin/products/_form",
:insert_after => "div.clearfix",
:partial => "partials/admin",
:name => "add_display_value")
and the partial:
<div>
<%= f.field_container :display_value do %>
<%= f.label :display_value, t(:display_value) %>
<br>
<%= f.text_field :display_value, :size => 30, :value => #product.display_value %>
<% end %>
<div>
I am using a form_for in one of my views in my rails app, but for some reason the closing tag is not being generated, and this will be handled safely in most browsers, except IE 8 and lower.
Here's some sample code:
<%= form_for object, :remote => true, :url => remote_update_path, :html => {:name => "form_#{id_number}", :id => "form_#{id_number}"} do |f| %>
<%= hidden_field_tag "field_a", object[:field_a] %>
<ul class="class1">
<li> <%= f.check_box :field_b, :class => "class2", :id => "b" %> B</li>
<li> <%= f.check_box :field_c, :class => "class2", :id => "c" %> C</li>
</ul>
<% end %>
For some reason this isn't generating the closing </form> tag where <% end %> is located. (I know there is now submit button in the form, this does not affect the missing </form>.)
Is this a bug? or is there something that I'm doing wrong?
This is almost certainly happening as a result of invalid HTML somewhere on the page. Run it through the validator, until it's all passing, and I bet the problem goes away...
I had the same issue. It happened to me because I didn't close one div tag properly inside the forms.
this form_for used to work before I ported my application to rails 3.1
<div class="form-box" style="padding-left:1em;">
<%
action = #existing_mass.nil? ? "add_to_power_plant": "update_power_plant_substrate";
submit_button_label = #existing_mass.nil? ? 'Add': 'Update';
%>
<%= form_for :substrate_mass, #substrate_mass, :remote => true, :url => { :action => action, :substrate_id => #substrate_mass.substrate } do |f| %>
<div>
<%= f.label :quantity_per_year, "Quantity" %>
<%= f.text_field :quantity_per_year, :size => 5, :onclick => 'this.select();', :value => #substrate_mass.quantity_per_year %>
</div>
<div class="actions" style="float:right;">
<%= f.submit submit_button_label %>
</div>
<br/>
<% end %>
</div>
I have spent over 4 hours trying to figure out what's wrong ... there is definitely something I am not understanding anymore
I get the error:
wrong number of arguments (3 for 2)
Note that I am trying to update an variable that is not an activerecord object. It's just an object that is not stored in the database.
Hope someone can help.
cheers
form_for only takes two arguments, the record, and options, although record may be several things, including a simple symbol, an object, or an array.
Try just dropping the first symbol and sending your object. If you model does not include ActiveModel::Naming, you may set the name via the :as option.
<%= form_for #substrate_mass, :as => 'substrate_mass', ... %>
More help may be found here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Or to view the source directly:
https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_view/helpers/form_helper.rb#L353
I just started working on a Ruby On Rails project and i got to the point where i need to see the contacts of a company.
They should appear once the company is selected..
<p>
<%= f.label :empresa_id %><br />
<%= f.select(:empresa_id, #empresa.map {|e| [e.nombre, e.id]} )%>
</p>
<%= observe_field :empresa_id, :url=>{:action => "get_contactos",
:controller=> :contactos, :updatewith =>:empresa_id} %>
But nothing happens, i don't see even a error on the script/server.
Can someone point me in the right direction?
http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-elements-in-rails-using-ajax/
I see on the link that a guy did the exact same thing i need but did not post any info.
Cheers.
Kinda got this working.. here's my update..
This is the form were my select boxes are placed:
">
"contacto_id",
:with => "empresa_id", :url => { :controller => "contactos",
:action => "get_contactos" } %>
The function get_contactos on the contactos controller :
def get_contactos
#contacto = Contacto.find_all_by_empresa_id(params[:empresa_id])
render :layout => false
end
And the view get_contactos.rhtml (contactos):
">%>
I just need to bind the form to the correct fields and the job will be finished.
I couldn't get this to work by observing the field :empresa_id but "empresa_id" works..
Edit: Got it to work.
I just binded the field on the form to the proper ones on the model and now i have the data =)
">
"llamada_contacto_id",
:with => "empresa_id", :url => { :controller => "contactos",
:action => "get_contactos" } %>
I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.