.js.erb file won't precompile - javascript

When I try to run rake assets precompile I get this error: Unexpected token: operator (>).
I figured out it was because of index.js.erb, because changing the name of the file to .html.erb made it compile.
This is the content of index.js.erb:
$('#haikus').append('<%= j render(#haikus) %>');
<% if #haikus.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(#haikus) %>');
<% else %>
$('.pagination').remove();
<% end %>
haikus.js.coffee:
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 200
$('.pagination').html('Loading...')
$.getScript(url)
$(window).scroll()
index.html.erb:
<div id="haikus">
<%= render #haikus %>
</div>
<div class="row pagination">
<div class="large-12 columns">
<%= will_paginate #haikus %>
</div>
</div>
How can I fix it and make it compile?
Thanks!

I suspect this has a lot to do with it:
changing the name of the file to .html.erb made it compile
Have you tried removing the logic from your js.erb file? I really thinking about the "will_paginate" stuff not able to be called from a javascript file. Maybe you could just have this line in the file & see if that compiles?
$('#haikus').append('<%= j render(#haikus) %>');
If that doesn't work, then we know it's something to do with this; else it will be something to do with the will_paginate most likely

Related

how to use Kaminari pagination in header

I'm trying to create a notification center that have pagination(via Kaminari)
like facebook.
I want this center to show pagination from any page(this center is contained in a header file).
Now I've written code below, then it shows appropriately when I access to home.html
But its pagination doesn't work in other page (eg. views/test/test.html.erb).
Probably, ajax seems cannot read js file so if I put test.js.erb file in the views/test, it will work.
In views of operation and maintenance, that is undesirable way.
How can I read pagination by using only one js.erb file? thank you for your helping
Source code is below.
Model
app/models/item.rb
class Item < ActiveRecord::Base
paginates_per 50
end
Controller
app/controller/items_controller.rb
before_action :pagination
def pagination
#items = Item.page params[:page]
end
View
app/views/static/home.html.erb
<%= render 'static/header' %> <!-- reading partial header-->
app/views/static/_header.html.erb
<div><%= render 'application/items', items:#items %></div>
app/views/static/home.js.erb
$('#items').append("<%= escape_javascript(render '_items', object: #items) %>");
$("#more_link").replaceWith("<%= escape_javascript(link_to_next_page(#items, 'more', remote: true, id: 'more_link')) %>");
app/views/application/_items.html.erb
<div id="items">
<%= render 'items' %>
</div>
<%= link_to_next_page #items, 'more', remote: true, id: 'more_link' %>
app/views/application/__items.html.erb
<% #items.each do |item| %>
<div><%= item.name %></div>
<% end %>
I'd suggest creating a separate controller action to handle ajax requests:
class ItemsController
def paginate
end
end
then move js.erb code under views/items/paginate.js.erb.
Kaminari's link_to_next_page helper uses link_to inside so it should be possible to add controller and action params to link:
<%=
link_to_next_page #items, 'more',
controller: 'items',
action: 'paginate',
remote: true,
id: 'more_link'
%>

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 %>

How to load partial into view when a link is clicked?

I have a list of user 'submissions' in my Rails app, and when a user submission is clicked, I would like the full submission to load into the view, without having to go to a new page.
Here's the code for the list of submissions:
<div id="submission-list-container">
<% current_user.submissions.each do |i| %>
<a href='#'>
<div id="post-container">
<%= i.title %>
</div>
</a>
<% end %>
</div>
The partial I have created, <%= render "show", :submission => i %>,, works fine, but I would like the full submission to be loaded into the view (index.html.erb), when that link above is clicked. Is there a good method for doing this? Should I just do something else like an AJAX call in JavaScript? I like these partials because it feels more clean and organized to seperate code.
My partial is pretty simple at the moment:
<%= submission.title %>
<%= dat_markdown(submission.content) %>
You can do this using a TitlePane in the Dojo Toolkit, and probably there's a similar widget with jQueryUI.
I can only speak to the Dojo Toolkit's version
You would do something like:
<div data-dojo-type="dijit/TitlePane" data-dojo-props="href: '/blah', title: '<%= submission.title %>', open: false">
You can include this in your application layout for lite usage of dojo, loaded from a CDN:
<script data-dojo-config="async: true, parseOnLoad: true"></script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js"></script>
The TitlePane is wired up so that when it is expanded, it will autoload the content that is provided to the href parameter.
While this isn't exactly a rails solution directly, it can be used as an alternative.
For a Rails Solution, you can simply use an AJAX call to a controller that renders the partial. You may even be able to hook it up using the
def blah
#submission = Submission.find(...)
respond_to do |format|
format.html # default render
format.js # js behavior
end
end
then a blah.html.erb
<%= render partial: 'submission/submission', object: #submission %>
with the partial
<div id='submission-<%= submission.id %>'>
<div id='submission-<%= submission.id %>-title'><%= link_to(submission.title, 'blah/blah', remote: true) %></div>
<div id='submission-<%= submission.id %>-content'></div>
</div>
and a blah.js.erb
$.get('/submission/content', function(data) { $('#submission-<%= #submission.id %>-content').html(data) } );
and an entry into the submissions_controller with route
def content
#submission = Submission.find(...)
render text: #submission.content
end
This probably isn't an exact solution, but hopefully it'll put you on the right path.

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