erb

Ruby on rails: using javascript variable in <%= %> tags in Html.erb

时光总嘲笑我的痴心妄想 提交于 2019-12-10 03:00:51
问题 I am trying to use insert an Javascript variable in the <%= %> tags but it prints the variable name verbose. Here is my code <script> function getGraph(agency,device_id) { var i = document.createElement('img'); i.src = '<%= show_graph_hcfcd_url('device_id') %>'; $(graphDiv).appendChild(i); } </script> Now the Problem is URL gets generated just fine except instead of value of device_id, 'device_id' appears in the rest url. Any clues how to get over this? 回答1: <%= %> will interpret the code on

How to include a subtemplate in a Puppet template

笑着哭i 提交于 2019-12-09 19:43:33
问题 I'm trying to include a subtemplate in a template for one of my Puppet modules. Here's how I do the include in my base template: <% def import(fname) erb = File.open(File.dirname(__FILE__) + "/" + fname) { |fp| ERB.new(fp.read) } erb.run end %> <%= import("subtemplate.erb") %> This works fine if I render it with erb command-line tool. However, during the Puppet run the import is silently ignored, i.e. the base template is rendered with a blank line where the rendered subtemplate should be.

recognize Ruby code in Treetop grammar

…衆ロ難τιáo~ 提交于 2019-12-09 16:39:08
问题 I'm trying to use Treetop to parse an ERB file. I need to be able to handle lines like the following: <% ruby_code_here %> <%= other_ruby_code %> Since Treetop is written in Ruby, and you write Treetop grammars in Ruby, is there already some existing way in Treetop to say "hey, look for Ruby code here, and give me its breakdown" without me having to write out separate rules to handle all parts of the Ruby language? I'm looking for a way, in my .treetop grammar file, to have something like:

How do I use .html.erb as a file extension for my views with Sinatra?

左心房为你撑大大i 提交于 2019-12-09 15:56:59
问题 If I have the following Sinatra code: get '/hi' do erb :hello end This works great if I have a file called views/hello.erb . However if I have a file called views/hello.html.erb Sinatra can't find the file and gives me an error. How do I tell Sinatra I want it to look for .html.erb as a valid .erb extension? 回答1: Sinatra uses Tilt to render its templates, and to associate extensions with them. All you have to do is tell Tilt it should use ERB to render that extension: Tilt.register Tilt:

Rails, jQuery, .js.erb files, JS not executed by the browser

假装没事ソ 提交于 2019-12-09 09:13:50
问题 I'm trying to use jQuery, and everything has been great, until now, when I'm trying to render a partial and append it to a div. Here is how I have it set up: I have an action that responds to js: def index @objects = Object.find(:all) respond_to do |format| format.js end end And a template called index.js.erb with some javascript in it: alert("hello world"); Firebug returns a "text/javascript" response containing: alert("hello world"); But the alert window does not appear, nor does any other

How do I run multiple lines of Ruby in html.erb file

瘦欲@ 提交于 2019-12-09 07:43:08
问题 I'm using Ruby on Rails and need to run a block of Ruby code in one of my html.erb files. Do I do it like this: <% def name %> <% name = username %> <%= name %> or like this: <% def name name = username %> <%= name %> Thanks for reading. 回答1: It is unusual to define a method in an ERB file, so I recommend against it. If you want to call a block like #each , you can do something like the following: <% names.each do |name| %> <%= name %> <% end %> Don't forget the <% end %> . 回答2: If you need

Loop in Ruby on Rails html.erb file

ⅰ亾dé卋堺 提交于 2019-12-09 07:38:05
问题 everybody I'm brand new with Ruby on Rails and I need to understand something. I have an instance variable (@users) and I need to loop over it inside an html.erb file a limitated number of times. I already used this: <% @users.each do |users| %> <%= do something %> <%end %> But I need to limitate it to, let's say, 10 times. What can I do? 回答1: If @users has more elements than you want to loop over, you can use first or slice : Using first <% @users.first(10).each do |users| %> <%= do

Best way to display flash notices in Rails

陌路散爱 提交于 2019-12-09 07:11:29
问题 I'm using notices in Rails and Devise and wondered what's the best way to display them? In my application I also have it so when a user saves a post, it saves it using Ajax and I use jQuery to display a notice. For all the notices I use: <% flash.each do |key,msg| %> <div class="message" id="<%= key %>"> <div class="message-inner"> <%= content_tag :div, msg %> <a class="dismiss">x</a> </div> </div> <% end %> The code for that is: $("#notice").html("Saved."); I'd like it to be a flash notice

Embedding an ejs template inside of an erb template

折月煮酒 提交于 2019-12-09 06:27:25
问题 I'm building a javascript-heavy rails 3 app. It uses underscore.js, which has a very elegant templating mechanism built on top of ejs ( http://embeddedjs.com/). The problem: embeddedjs borrows heavily from the erb syntax, so including ejs templates in an erb template causes rendering problems with the view. Is there a way to include "non-erb" sections in an erb file? This would let me define ejs templates inside erb files. Right now I'm using a hack where I have a helper that reads the raw

Rails syntax error: unexpected keyword_ensure, expecting keyword_end

无人久伴 提交于 2019-12-09 03:34:29
问题 <h1>Get Ready</h1> <% if params[:ballot_position].to_i > 1 %> <p> Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %> and switch places with them. </p> <p> Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below. </p> <% @ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %> <a href="<%= @ballot_link %>" class="btn btn-primary">Ready</a> Above code seems to be resulting in: ready