visual_effect after replace_html not working in rjs - ruby-on-rails

In learning ruby on rails I've created a blog site. When the user adds a post via AJAX, the following rjs gets called:
page.replace_html 'posts', :partial => #posts
page.visual_effect :Highlight, 'post_' + #post.id.to_s
However, the highlight isn't happening.. neither is any sort of effect even a hide.
Clues:
It works if I just do an insert_html
for just the new post (but I want
to update the whole list of posts
at this time)
If I hard code the id to the next id in the sequence, it doesn't work on the post, but it does work on the next post. { ex.. hardcode post_100... no highlight on submit 100, highlight 100 on submit 101 }
Alert shows that 'post_' + #post.id.to_s is what is expected ( post_100, etc )
Any ideas or debugging suggestions?
Thanks,
Orlando

Can you alert the innerHTML of the $("post_#{#post.id}") before the visual_effect.
Does firebug give you an error when it gets to the visual_effect?
Can you do something else, like an alert after the visual_effect line?
Have you got the required js files included?

It's not really an answer to the problem, but I have since done away with reliance on rjs. Instead I'm following the pattern outlined here
http://blog.solnic.eu/2007/10/30/why-javascript-helpers-in-rails-are-evil
And now all effects are working as expected. Note that I did get the effect working when comments were added using effectively the same code that should have been working here, so I'm fairly convinced there was just some sort of weird operator error going on.

Related

joosy forms don't work

I follow guide http://guides.joosy.ws/guides/blog/forms.html and try to create forms for new and edit post
So I create templates/pages/posts/_form.jst.hamlc with form and add code in pages/posts/new.js.coffee:
Joosy.namespace 'Posts', ->
class #NewPage extends ApplicationPage
#layout ApplicationLayout
#view 'new'
elements:
'rawForm': 'form'
#fetch (complete) ->
#data.post = Post.build()
complete()
#afterLoad ->
#form = Joosy.Form.attach #rawForm,
resource: #data.post
success: (data) => #navigate '/'
But "inner magic" doesn't work for me.. When I follow http://localhost:3000/blog#!/posts/new I see form for new post. This form submits as usual form. I.e. if I type something and press Save, it opens new page http://localhost:3000/blog?post%5Btitle%5D=test&post%5Bbody%5D=test#!/posts
So, I've stopped on this step and have no ideas what to do with that..
P.S. If you can retag this question to 'joosy' please do it. Author promises to answer questions about joosy framework here by 'joosy' tag, but I can't create this tag due to lack of reputation
You should check three things:
Check console for overall errors. It may fail before afterLoad. And this is javascript which just dies silently unless you watch errors.
Try to add console.log #rawForm in your afterLoad to ensure you get the correct DOM element binded.
Try to compare your code with working example: https://github.com/roundlake/joosy-example
And if 1/2 didn't work and you don't have much time, show me your template either cause this pages looks like a copy of this one: https://github.com/roundlake/joosy-example/blob/master/app/assets/javascripts/blog/pages/posts/new.js.coffee. But the latter is known to work. So trouble is not here.

Capybara not able to recognize ajax loaded elements.?

In my Rails 3 application, I have a "image" on my HTML page, which creates a "div" element in html page, when clicked.
I have to test the creation of this new "div" through RSpec (and i am using Capybara for views based testing).
I have written the following code :
it "clicks the extended details button" do
Capybara.default_wait_time = 5
within('.table_expand') do
find("#img_dealer_code_04039").click
should have_selector('#extended_details_04039')
end
end
yeah i have already added, :js=>true in corresponding describe.
I thought the problem would be of Ajax time , so i added Capybara.default_wait_time = 5 for it to load properly. (But it didnt work)
Also i tried putting :visible=>true in line should have_selector('#extended_details_04039') , but with no success..
Is there anything that i am missing..??
Please help me...I am stuck with this from quite a long time..!!
https://github.com/rspec/rspec-rails/issues/478
are you sure that you don't have duplicated ID on site?
Try to save it to file and check the source for duplications.
To see what's going on the page try
page.save_and_open_page method
Is img part of your ID or is it the tag type? I believe you shoul have
find("img#dealer_code_0409").click

add item with ajax

im trying add item with ajax, all work, except the view doesnt refresh, when i force refresh, new product in the list, here is code http://pastie.org/1533605
what's wrong? im using jquery
okay, i figure it out, some problem with my markup
sry of this stupid question
$('<%= escape_javascript(render(:partial => #product))%>').insertBefore('.height');
that was final solution
Try:
$("#products").html("<%= escape_javascript(render(:partial => "products")) %>");
And I guess you'd better do $("#products").append(...) so that ou keep the former content instead of replacing it.
To conclude, consider keeping this kind of behavior on the client side, look at: http://railscasts.com/episodes/197-nested-model-form-part-2

Rails Link_To doesn't add a CSS Class correctly

So I'm trying to use link_to to create a link in my Rails app and trying to add a CSS class to certain links. The problem that I have is that when I add my html options to the link_to arguments, the links don't get created and I end up with nothing. Here's my code:
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :html=>{:class=>"work"}) %>
I also tried variations of this and it still didn't work. For example,
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :class=>"work") %>
The method also exhibits some strange behavior, which I think might be the culprit. If I go straight to the page, no POST or GET requests, link_to works properly and the links and images render correctly, which is to say that they actually DO render. However, the way that I would like to get to the page is by form POST request in a previous page whose action is the results page I'm trying to get to.
Thanks for any help you can provide!
EDIT: I'm not sure exactly what the problem was, but I solved it by changing the form's method to GET instead of POST.
It's most likely because the POST request is hitting a different method (new), rather than (show). You must supply the proper instance variables to the view. You seem to be referencing both #beetle and beetle. Have a look at all of those variables, since that appears to be the POST problem.

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