How can I call an action, pass a value to the action from js and re-render the js on a page in rails 3?
I have a page poweroutput.html.erb whose controller action is as follows:
def poweroutput
# other stuff here...
if (request.xhr?)
@date = :date
respond_to do |format|
format.html
end
else
@date = Time.now.getlocal.strftime("%d.%m.%Y")
end
end
The else
clause is executing fine on loading the page. I am expecting the if
clause to execute on change of a text field element in my poweroutput.html.erb which is as follows:
<%= text_field_tag(
'dt', nil,
{ :id => 'date-field',
:class => 'dateformat-d-dt-m-dt-Y',
:value => Time.now.getlocal.strftime("%d.%m.%Y"),
:onchange => remote_function(
:url => {:controller => 'pages', :action => 'poweroutput'}, :with => "'date=' + $('date-field').value"),
})
%>
So what I'd like to happen onchange of the text field is: (1) controller action, poweroutput, to be called, (2) for the contents of this text field be assigned to @date instance variable in the controller action and (3) for the javascript (located within script tags in poweroutput.html.erb) to be re-rendered (is re-rendered even the right word?). I'm not even su开发者_如何转开发re I'm on the right path here. Please tell me if there is a better way to do this. The nonfunctional code you see above is just the product of two days of google searching. Any help would be greatly appreciated.
As I noticed in your remote_function
you don't specify the element to replace with the answer.
More info is here http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/remote_function
精彩评论