"Syntax Error" is displayed due to change in curly braces - javascript

I'm trying to run this code using node.js
This code shows a "Syntax Error" when I run the code below.
<% friends.forEach(function(friend) %>
<% { %>
<li>I have a friend: <strong><%= friend %></strong></li>
<% }); %>
But it seems to work fine if I change the opening curly brace to this syntax.
<% friends.forEach(function(friend){ %>
<li>I have a friend: <strong><%= friend %></strong></li>
<% }); %>
Can someone please explain why this happens? I am using an ejs template engine.

Related

How to show some stuff or a message beetween the main content in rails

I have a rails app where users can Sign Up/Sign In and post stories, follow other authors, what i want to achieve is how to put some content like "Authors to follow" in between of the main content while the user is scrolling (Like what twitter does with "Who to follow".
with react i have created a User_Follow_suggestion_container:
<%= react_component('FollowSuggestionsContainer') %>
It works fine, now i want to know, where to place it, so it can come randomly or after scrolling down to 20 or 30 posts.
Assuming your react component is inside a loop, you'd do something like this:
<%- #tweets.each_with_index do |tweet, index| %>
<%= tweet %>
<%- if index == 20 ( or 30 or even rand(20..30)) %>
<%= react_component('FollowSuggestionsContainer') %>
Sorry if the syntax is wrong. I haven't used ERB in years..
Edit: If you want it after scroll, you'd probably do something with JavaScript instead of Ruby/Rails.

Migrating plugins from Redmine 1.x to 2.x - Javascript does not work

Sorry about my English. I have migrated plugins to Redmine 2.x, but Javascript functions does not work.
At the top of one of my plugins show.html.erb:
<% content_for :javascript_includes do %>
<%= javascript_include_tag 'hgp_markers.js', :plugin => 'hgp_markers' %>
<% end %>
hgp_markers.js:
function hideEditableMarkers() {
$$('.edit_marker').each(function(el) {
el.hide();
});
$$('.show_marker').each(function(el) {
el.show();
});
}
Event.observe(window, 'load', hideEditableMarkers);
And this line with javascript:
<%= link_to_function l(:button_edit), "hideEditableMarkers(); $('marker_#{m.id}_row').hide(); $('marker_#{m.id}_row_edit').show(); return false;", :class => 'icon icon-edit' %>
What am I doing wrong?
thanks in advance
Redmine 2.x (Rails 3.x) switches from Prototype to jQuery. So you need to migrate too.
Thus, Event.observe is a Prototype function.
See also: http://projects.andriylesyuk.com/projects/base/wiki/Porting-to-Redmine-2

Integers Being Added to Array

I'm trying to use an array of objects to simplify maintaining a list of HTML buttons. Here's my code:
<% links = [{url:"https://github.com/drguildo", icon:"github"}, %>
<% {url:"http://www.flickr.com/photos/drguildo/", icon:"flickr"}, %>
<% {url:"http://instagram.com/therac25", icon:"instagram"}, %>
<% {url:"http://www.last.fm/user/drguildo", icon:"lastfm"}] %>
<% for link in links: %>
<img src="/img/icons/<%= link.icon %>.png" />
<% end %>
The problem is the resulting array looks like this:
[object Object],16,[object Object],17,[object Object],18,[object Object]
which messes up the output. Why are the objects interspersed with integers and how can I prevent it?
I'm probably doing this in a very sub-optimal way (I'm new to DocPad, ECO and CoffeeScript) so any suggestions on how to improve my code would be appreciated.
You don't want to be creating an array like this within the template. The point of eco templates it to separate the logic/data from the presentation. Declare the data separately, then use it to render the template.
Like this
eco = require "eco"
fs = require "fs"
template = fs.readFileSync __dirname + "/views/test.html.eco", "utf-8"
console.log eco.render template, links: [
{url:"https://github.com/drguildo", icon:"github"},
{url:"http://www.flickr.com/photos/drguildo/", icon:"flickr"},
{url:"http://instagram.com/therac25", icon:"instagram"},
{url:"http://www.last.fm/user/drguildo", icon:"lastfm"}
]
and then just
<% for link in #links: %>
<img src="/img/icons/<%= link.icon %>.png" />
<% end %>

backbone templates: index.jst.eco to index.jst.ejs

Specific question: this code works in a backbone template index.jst.eco, but it doesn't work in index.jst.ejs
<ui>
<% for entry in #entries.models: %>
<li> <%= entry.get('name') %></li>
<% end %>
</ui>
I'd like to know why (i.e. how to fix it for ejs), and, more generally, is there comprehensive documentation for how to write code in ejs templates? I can't find anything that goes into detail. As I've been playing around with ejs and eco, I've noticed the presence or absence of a : or a bracket can make a huge impact but I can't figure out how to know what to use and when.
I'm willing to use jst.eco or jst.ejs depending on which syntax has better documentation.
Just encountered this problem, here's my solution!
<% entries.each(function(entry){%>
<li><%= entry.get('name') %></li>
<% });%>
This is how that eco code would be written in jst.ejs.
<% for (var i = 0; i < entries.length; i++) { %>
<li> <%= entries.models[i].get('name') %></li>
<% } %>
However, I can't get it to work with a for entry in entries.models iterator
From what I've known, jst.eco format is CoffeeScript embedded into jst template, and jst.ejs will only work with JavaScript. That's why in your case it can render this JavaScript iteration:
<% for (var i = 0; i < entries.length; i++) { %>
but not this CoffeeScript interation:
<% for entry in #entries.models: %>

jquery ajax rendering of a new post in ruby on rails

I'm trying to render the creation of a post with jquery ajax but I can't seem to get it to work correctly. It seems to render the post with very strange (I think nonexistent) styling and the flash message appears only after the page is refreshed. The post also get formatted correctly after the page is refreshed.
Here's the javascript:
$("#micropost_form").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice))%></div>');
$("#user_info").html("<%= pluralize(current_user.microposts.count, "micropost") %>");
$("#feed_items").prepend(" <%= escape_javascript(render(:partial => #micropost)) %>")
$("#micropost_form")[0].reset();
The body of my application layout:
<body>
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<div id="flash_notice"><%= render 'shared/flash_messages'%></div>
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
Here's my home page (rendered in the yield of the application layout):
<table class="front" summary="For signed-in users">
<tr>
<td class="main">
<h1 class="micropost">What's happening?</h1>
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
Here's my feed partial:
<% unless #feed_items.empty? %>
<table id="feed_items" class="microposts" summary="User microposts">
<%= render :partial => 'shared/feed_item', :collection => #feed_items %>
</table>
<%= will_paginate #feed_items %>
<% end %>
My flash messages partial:
<% flash.each do |key, value| %>
<div class="<%= key %>"><%= value %></div>
<% end %>
Let me know if there's any other code I should post.
So what am I doing wrong? If you want to look at all the code and run it locally to get a better understanding of what's happening, you can find it here: https://github.com/meltzerj/sample_app
When you're using ajax, you need to use flash.now for flash messages. See docs here. Rails is looking for a page refresh/redirect to push and pop from the flash hash stack, using now will set it immediately, then you can access it as above. You can use flash.now in replace of your regular flash usage in the controller. Something like this is very typical:
if #object.save
flash.now[:notice] = "Object successfully created"
else
...
end
So that will fix your flash issue.
As for the weird content/styling, it's a bit hard to comment without actually seeing the markup. The only thing that looks odd to me is the render :partial => #micropost
I'm not sure if #micropost is just a string, but you'd normally do something like render :partial => 'path/to/partial', :object => #micropost
Again it's almost impossible to say without seeing all the other markup

Categories

Resources