previously i had javascript code right in the top of my view and it would work. Then, i learned that you can include the js file using yield and i did just that.
I had rails view code inside my javascript code like :
$( "#exp-progressbar" ).progressbar({
value: <%= ((#quest.end_time - Time.now).to_i * 100 ) / (#quest.duration * 60) %>
});
Now, after i do it with yield, i get an error when i do that in the separate js file. How can i do it now ?
I found the solution :
<% content_for :javascript_includes do %>
<% if not #quest.nil? %>
<script type="text/javascript">
var experience = <%= ((#quest.end_time - Time.now).to_i * 100 ) / (#quest.duration * 60) %>;
var quest_duration = <%= #quest.duration * 60 %>;
var quest_timer = <%= (#quest.end_time - Time.now).to_i %>;
</script>
<%= javascript_include_tag "experience.js" %>
<% end %>
<% end %>
<% if flash[:error] %>
<%= flash[:error] %><br><br>
<% end %>
Related
I try in ejs to compare two Strings which in the current situation are the same and it runs the else case. Here is the code:
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf == ids){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
It logs 5d5ecfd1ad6d193de86c2264 - 5d5ecfd1ad6d193de86c2264
Use === instead of == to see if this fixes the issue, as it will prevent internal type coercion.
It might also be worth trimming the strings to see if there is any white space or padding added to them.
You could use the below code to compare two string values in ejs
<% if("some text" === "some text"){ %>
<h1>Yes the above text are equal</h1>
<% } %>
If you are comparing mongoDB objectId, then use toString().
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf.toString() == ids.toString()){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
I have a partial where I'd like to drop or not show the first three articles in the array because they are in a featured articles section. I also want the partial to use will_paginate w/ endless scrolling to load the next page of articles. The issue I'm facing is that when using #articles.drop(3).each do |a| and the next page goes to load, the array drops the next three articles again.
What's the best way to solve for this? My initial thought was an array within an array, where the first array drops the first 3 then the nested array returns all articles but I'm not sure how to do that?
Array code in partial:
<% #articles.drop(3).each do |a| %>
<%= link_to a.source_url, :class => "flexRow" do %>
<%= a.source %>
<h3><%= a.title %></h3>
<% end %>
<% end %>
Index.js.erb
$('#article-index').append(' <%= j render("articles") %>');
<% if #articles .next_page %>
$('.pagination').replaceWith('<%= j will_paginate(#articles, :previous_label => '', :next_label => '', :page_links => false) %>');
<% else %>
$('.pagination').remove();
<% end %>
Index.html.erb
<div id="article-index">
<%= render 'articles' %>
</div>
UPDATE
This solution seems to work but doesn't feel elegant?
<% (#articles.current_page == 1 ? #articles.drop(3) : #articles).each do |a| %>
Try
#articles[3..#articles.count]
This will drop the records held at index 0, 1 and 2, and return the remaining.
You may do the following in your controller:
EXAMPLE
#articles = Article.where(...).paginate(page: params[:page], per_page: 10)
# Works only for the first HTML request
unless request.xhr?
#articles.shift(3)
end
.
.
.
respond_to do |format|
format.html
format.js
end
Now, when you iterate over #articles, it would start from index 3, only for first time.
EDIT
<% (request.xhr? ? #articles : #articles[3..10]).each do |a| %>
<%= link_to a.source_url, :class => "flexRow" do %>
<%= a.source %>
<h3><%= a.title %></h3>
<% end %>
<% end %>
Assuming the page size is 10.
Multiplyselect is forgetting owners values after searching.
After proceed i got params[:search] and params[:owners] but only input for search is filled-in. This is my code.
def index
#all_owners = Owner.select('distinct name').pluck(:name)
#animal = Animal.search(params[:search])
#animal = #animals.joins(:owners).where("owners.name IN (?) ", params[:owners].present? ? params[:owners] : #owners)
end
#------------------------------------------
<%= form_tag animals_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search]%>
<%= select_tag :owners, options_for_select(#all_owners),id: "multiselect-id", multiple: true %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #aminals.each do |animal| %>
<%= animal.name %>
<%= animal.owners.map(&:name).join(', ') %>
<% end %>
<script type="text/javascript">
$(document).ready(function() {
$('#multiselect-id').select2();
});
</script>
You forgot to specify the currently selected values in the select_tag. This is done e.g. by a second argument to the options_for_select helper, i.e. something like: options_for_select(#all_owners, params[:owners] || #owners).
See the docs here.
How can I render a `.each do block in a js partial in Rails?
In my messages.js.erb file, I currently have:
<% #conversations.each do |convo| %>
<%= j render "message_listing", convo: convo %>
<% end %>
$('#conversations-body-container').append(
// APPEND RESULTS OF PREVIOUS .EACH DO RENDER
);
How can I go about appending the first block of code in my HTML using j render?
You could do like below:
<% #conversations.each do |convo| %>
$('#conversations-body-container').append('<%= j render "message_listing", convo: convo %>');
<% end %>
Or
<% #conversations.each do |convo| %>
$('<%= j render "message_listing", convo: convo %>').appendTo('#conversations-body-container');
<% end %>
I'm having some problems with embedded ruby. I'm trying to iterate through some variable, to print some JavaScript code.
This is the following code:
<% color1 = '#ff0000' %>
<% color2 = '#00ff00' %>
<% color3 = '#ffff00' %>
<% i = 0 %>
<% j = 0 %>
<% #chapters.each do |chapter| %>
sigInst.addNode('cap<%= chapter.reference %>',{
label: 'cap <%= chapter.reference %>',
color: '<%= j%2==0 ? color1 : color2 %>',
x: <%= i = i %>,
y: <%= i = i %>
}).draw();
<% i = i + 0.1 %>
<% j=j+1 %>
<% end %>
<% #chapters.each do |chapter| %>
<% chapter.decisions do |decision| %>
sigInst.addEdge('cap<%= chapter.reference %>_cap<%= decision.destiny_num %>','cap<%= chapter.reference %>','cap<%= decision.destiny_num %>').draw();
<% end %>
<% end %>
I'm using Sigma js to implement a graph. The first .each appears in the view, but the second part doesn't print anything. What's happening? Thanks
You forgot to do a .each on your decisions.
So the fixed line would be: <% chapter.decisions.each do |decision| %>
Ruby has the sometimes annoying feature of allowing you to pass a block to anything, whether it uses it or not. So in this case, you're just passing the block to the decisions method, which does nothing with it.