jQuery doenst work in a javascript ruby tag?
I've put this problem up before but i way I was trying to do it was terrible to say the least. I need to call a jQuery show() method when a certain argument is fulfilled in a Ruby loop. My idea was a Javascript tag similar to the example. But for some reason jQuery does not work in this javascript tag. I can't think of a better way of doing this than the one im currently trying to do.
code:
<% @records.each do |record| %>
<% if record.task.project.name == "SKIP"%>
<%= javascript_tag :defer => 'defer' do -%>
$('.toil.).show();
<% end -%>
<% else %>
<%= javascript_tag :defer => 'defer' do -%>
$('.toil.).hide();
<% end -%>
this loop works and ive tested that. Is there a better way of trying to what i want? If there is a project with name skip then a class needs to be shown... if there arent any then it needs to be hidden. Seems simpl开发者_如何学JAVAe but complex...
You don't really explain the nature of what needs to be hidden or shown, so there's a bunch of ways to go about this.
If it's a section of HTML per-record, just don't render it in the loop:
<% @records.each do |record| %>
<% if record.task.project.name != "SKIP"%>
All the HTML
<% end %>
<% end %>
If it's something you still need rendered because it might become visible later, set a class:
<% @records.each do |record| %>
<% skipIt = record.task.project.name == "SKIP" ? "display: none;" : "" %>
<span style="<%= skipIt %>">
All the HTML
</span>
<% end %>
<% end %>
If it's just a flag to be used later:
<% skipIt = false %>
<% @records.each do |record| %>
<% skipIt = true if record.task.project.name == "SKIP" %>
All the HTML
<% end %>
<% end %>
...
$(function() {
<% if skipIt %>
$(".toil").hide();
<% end %>
});
Etc. Lots of ways. Depends on what you really need, which we don't know.
(In most of the examples above, functionality should probably be moved into a partial or at least a helper.)
To begin with, there is a typo there:
$('.toil.).show();
You are missing a single quote at the end of the selectors.
They should be for example:
$('.toil').show();
That will show all the elements with a css class named "toil". Javascript and jQuery work with HTML and CSS, I have the feeling you are trying to read or manipulate ruby tags, and that won't work, because JS is executed when the data arrives to the browser, so the ruby tags have been already processed and converted to html just in case :D
精彩评论