render :js not working - ruby-on-rails

I am working on window 7 and facing issue with render
render :js => "alert('Hello')".
But it is not getting alert.
Do any one have experience with this issue.

You should make the call using AJAX so you should add the param "remote: true" to the link or form that is making the call to that action

Be sure that there is client side requested with JS format, as in that cases Rails set up content type for js, and not text/html.

It seems like you are trying to debug your code. Using a before_filter is not the best way. A request can only render once.
I suggest adding statements to your logging, and using the javascript/development console of your browser (Chrome/Firefox): this will show what goes over the wire perfectly,a dn what is executed on the client, and why it fails (if it fails).
Hope this helps.

Related

Displaying flash message in portion of page that is otherwise not updated

I'm looking to display my flash messages in a portion of the page that is otherwise not always in a partial that gets updated.
In other words, I may submit a form that updates a partial via ajax. But I want to display the flash message in a portion of the page that is outside of that partial.
I could have some javascript in every single necessary js.erb file to update the flash partial, but that seems crazy. Is there a more simple way of going about this?
I don't have to necessarily use flash messages either if something custom would work better.
Thanks!
You can do it the low-tech way by using a :remote call on your form that, when executed, will inject some HTML back into your page from a partial of your choosing.
It's pretty easy to do in a .rjs view:
page['flash'].html(render(:partial => 'flash'))
You can also do it in a .js.erb view using jQuery:
$('#flash').html("<%= escape_javascript(render(:partial => 'flash')) %>");
I tend to think the .js.erb method is a lot more ugly, but we all have our preferences.

Page not getting rendered via Express when using post (NodeJS)

I am using Express v.2.4.6 (Node.js - v.0.6.2).
I am trying to render (or redirect to) a new page once POST is called (as opposed to GET). I am able to render/redirect when GET is called. However, I cannot seem to render a page when POST is called in Express. I am not sure if this is even possible though the guide on the Express site does mention an example where you can redirect once POST is called.
The relevant code is given below (client is sending the form in JSON). I can parse through the JSON message successfully in Node.j.
Sample route:
app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});
No exceptions are thrown, but the index page does not get rendered nor redirected.
Any feedback will be appreciated.
Maybe you call it from $.ajax from client-side
It works well if you call it from server-side
Please refer to this question.
Express.js Won't Render in Post Action
res.redirect('/') is most likely what you wanted there, but you can render in any route, redirecting is just a convention that most people use

Rails validation

I am suffering a issue with rails server side validation. Can some one help me out from this?
situation is :
I am creating dynamic for and its elements also dynamic.My application will generate the some HTMl code. Which can we use in any form or blog..
I applying the server side validation. But due to dynamic elements .I am not able to store the last entered value in to the elements. AS we normally does in PHP if user input something wrong we don't put the field empty. So I need to find a mechanism which fills the older values into the elements,If something went wrong.
This is the code into controller which is I'm using to show the form :
render :layout => false,:template=>'buildders/rander_form'
and view of rander_form.html.erb has
<%= render :file=>RAILS_ROOT+'/public/forms/form_'+#form_name+'.html.erb' %>
where #form_name is a dynamic form name(which have HTML code).
Can some one help me?
don't put erb files in public, people can download them by entering the file path in the url
also why not move that code out of the erb template into the controller?

AJAX request with Rails 3 and jQuery is being processed as HTML.

I have a form that i want submitting with AJAX, using rails 3 and jquery. The problem that I am facing is that rails is responding to the AJAX request as HTML too. After a little search I found that this has something to do with the correct Accept headers not being passed. How do i fix this?
This is the controller code
respond_to do |format|
format.js { render 'user/create' }
format.html { redirect_to ((params[:feed][:url].nil?)?url_for(:home) : params[:feed][:url]) }
end
It seems to work on a friends firefox, and on my chrome too, smthng wrong with my firefox?
UPDATE: It seems that the error arises only when I use a proxy service as JonDo, which probably changes the accept headers... Is there a way to force rails to use js format if the X-requested-by header is present?
Thanks
Your controller code looks correct. Are you sure that you have added the .js suffix to the AJAX URL in your form? That's how the responder knows what format you want for the response. The default format is HTML so omitting the suffix would look like it's responding to a HTML request.
You likely didn't set the dataType properly. See the docs (this is &.post) If you set dataType to "js" you'll be all good!

Agile web development with rails - Ajax

i m using rails 2.3.3 and web browser Firefox i have added ajax and java script and it is working too but i have to reload the page every time when i press Add to Cart button to show item additionn in the side bar it don’t show it without reloading.
anyone please help me how can it show item addition in side bar when i press Add to Cart button with out reloading the page?
If you haven't already done so, install Firebug for Firefox, for these reasons:
it'll tell you if you have a Javascript error.
it'll show you if your Ajax request is being received and its contents.
you can inspect your page elements such as the cart to see if it's set to be shown, if the ID is correct, etc. in a much faster way than browsing through the source.
and more (CSS, etc).
If you can't figure it out by looking at the Firebug console, and since you're following a tutorial, why don't you download the Depot source code and compare it with your own to see what you're doing wrong.
If you have the book, the complete source is listed at the end of the book. You can also download the source from here.
The standard ajax helper methods are link_to_remote, form_remote_tag, form_remote_for, button_for_remote. (I might have missed a few.) If you're not using one of them, you could be doing something wrong.
If you're using one of the helper methods with remote as part of the name, you might be missing the update option or the update option is pointed to the wrong html element.
For a form_remote_tag helper:
form_remote_tag(:url => {:controller => controller_name, :action => action_name, :id => id},
:update => element_to_update)
The element_to_update should be the html element's id that you're updating.

Resources