FAQ Database Discussion Community


ERB scope: instance variable vs locals

ruby-on-rails,ruby,performance,erb
Suppose I have a controller with an index action and I would like to pass some data into the action's view (index.html.erb). Typically, the rails way is to do: @some_var = some_value. Does the above add to the global scope in a sense that @some_var is now available everywhere (helpers,...

How to get the whole sentence for title of tag using <%= t('vote.up') %> in rails?

html,ruby-on-rails,erb
In en.yml, vote.up = "This question does not show any research effort; it is unclear or not useful" When I write: <a href="#" title=<%= t('vote.up') %> ><i class="icon-arrow-down"></i></a> I get the HTML: <a href="#" title="This" question="" does="" not="" show="" any="" research="" effort;="" it="" is="" unclear="" or="" useful=""><i class="icon-arrow-down"></i></a> But I...

Monkey patching scope: Outside function doesn't affect ERB

ruby-on-rails,ruby,erb,monkeypatching
I'm trying to patch some I18n methods to test correct escaping. Doing this in a test, and it goes something like: test "JS files should be using correct escaping" do module I18n def self.t(*args) %q{a JS interpolation ' " test} end end filename = Rails.root + "app/assets/some_file.js.erb" erb = ERB.new(File.read(filename))...

Would adding a dynamic tooltip affect load time?

ruby-on-rails,ruby,performance,erb,load-time
I have a table "State", consisting of a few thousand records, and I'm printing each of their names on one page. Would adding a dynamic tooltip to each add noticeably to the load time of the page? In other words, would there be a significant performance difference between these two...

Time_ago_in_words is not working

ruby-on-rails,ruby,ruby-on-rails-4,erb
I'm having a weird problem. In my rails app, doing: time_ago_in_words homework.created_at returns the error: undefined method `>' for nil:NilClass Context: <h3 class="media-heading"><%= homework.title %></h3> <p>Assigned <%= time_ago_in_words homework.created_at %> ago</p> created_at is defined, with this migration t.timestamps null: false (yes, I have ran it), and it's in the database....

Can't find the ERB gem

ruby,erb
I know how to use erb, and have done so on rails and in standalone ruby apps.. I don't know where it came from, as I have never installed it but it's there. I used rbenv on my mac. I installed ruby on a windows 8 computer using the rails...

How to replace a pattern in a string

ruby-on-rails,ruby,regex,string,erb
I built a page that is rendered differently depending on params. The logic is something like this: <% if params[:x] == "1" %> <!--render version A--> <% elsif params[:x] == "2" %> <!--render version B--> <% elsif params[:x] == "3" %> <!--render version C--> <% end %> I want each...

How do I iterate over a range in ERB?

html,ruby-on-rails,erb
I'm trying to generate a dropdown with the alphabet in it. Weird, I know, I'm making an old school arcade style name selector for a game (3/4 letters) selectable with arrow keys. So far I've got this: <%= select_tag(:letters) do %> <% ('A'..'Z').each do |letter| %> <%= content_tag(:option, letter, value:...

How to Dynamically name a Hash key in Ruby

ruby,dynamic,hash,key,erb
I'm trying to make a 'render' call in an erb file where the key of the hash is dynamically named. For example... <% object_type_abbr = ["pos", "job_description", "policy", "procedure", "step", "task", "product"] %> <%= render path.to_s, model_id: @model.id, object_type_abbr[i]: orphan, row_no: row_no, is_orphan: true %> The problem is this syntax...

How can I convert html.slim files to html or html.erb?

html,converter,erb,slim-lang
I need to convert the html.slim files in my Ruby on Rails application to html.erb. Is there any easy way to do it? I tried many options listed in Stack Overflow and other sites. But nothing worked for me. I found a converter http://slim2html.raving.systems/ and a html to slim converter...

Chef Template Nested Loops

erb,chef-recipe,cookbook
I have a small problem with my chef cookbook, i'm trying todo a nested loop (loop in loop). I have a array with multiple levels and i would like to loop all levels in a chef template. Right now i'm down at the following: Template: <% @countries.each do |country| %>...

My update.js.erb file only runs on the first button click and not on subsequent clicks (change a button's class and value on click)

javascript,jquery,ruby-on-rails,ruby,erb
I have a todo list app with a complete/incomplete button for tasks. I want to toggle between making the button class and button value change when I click it. I get the button to change the first click, but subsequent clicks to the same button do not alter the button....

:promt in Rails select() helper not working

ruby-on-rails,ruby-on-rails-4,erb
I have next string in my form template(from book 'Agile web development with Rails'): <%= f.select :pay_type, Order::PAYMENTS_TYPES, promt: 'Select a payment method' %> When form is being visited with browser, there is not default "promt" for select field indicated. What is the problem? UPD I'm using Rails v4.0.2 UPD...

Why is Index view printing entire table?

html,ruby-on-rails,sqlite,model-view-controller,erb
This app consists of a form to submit and currently I am trying to print a few rows of a table. This is working, but unfortunately I am also getting a single long string of the entire database table attributes. There is nothing in the code that I have written...

How can I add a whitespace to a string in erb?

html,ruby,erb
I'm displaying a link as a string in erb: <%= link_to (@teacher.id + 1).to_s, "#" %> How can I add one space at the beginning? If I do this: " " + (@teacher.id + 1).to_s, the space is ignored, and if I do this: "&nbsp" + (@teacher.id + 1).to_s or...

Database data displaying on html (rails)

html,ruby-on-rails,ruby,erb
I am making a rails app that users can use to shop for products. Everything was working fine, then I adjusted the products#index page, and now the data from the database is displaying on the html under the <div> of the page like so; [#&lt;Product id: 1, name: &quot;Green&quot;, description:...

Limit check_box_tags in rails application

ruby-on-rails,erb
I'm using the check_box_tag to setup checkboxes in my rails app (in an html.erb file, within a form) and i want to limit the user to selecting a maximum of 2. How can i achieve this? <% if @users != nil %> <% @users.each do |u| %> <%= check_box_tag 'user_ids[]'...

Sorting tweets by re-tweets/favourites

ruby,twitter,sinatra,erb
I'm trying to implement a method whereby a user can manipulate three radio buttons, each with assigned methods, to sort the tweets either by retweets, by favourites or default(by date). Currently, the page lists the tweets by date as default but every time I pressed either of the two buttons,...

What can cause Sprockets::CachedEnvironment error?

javascript,ruby-on-rails-4,tinymce,erb,centos6
My RoR application falls down with the following error: ActionView::Template::Error (undefined method `attributes_for' for Sprockets::CachedEnvironment:0x00000005baf3d8 As I found out this error happens when the javascript in the html.erb template is rendering. Also I noticed that in all templates where it happens exists tinyMCE initialization command. I suppose, that it is...

How to get an inline-block element to expand downward?

html,css,list,css3,erb
I have an erb block that prints a list with two elements, a .list-number and an .list-item, and I'm inserting ...<br> after every .list-item. I want the list to display like so: 1. Item1... 2. Item2... 3. Really long item3... 4. Really really really long item4... 5. Item5... But I'm...

How to rewrite HTML for Rails

html,ruby-on-rails,erb
I'm trying to re-write the code below so that it will use an image_tag. I know how to apply a class attribute, but how do I apply a custom attribute like "data-dark-logo"? I'm learning how to develop and am building an application that has the following HTML: <div id="logo"> <a...

Why isn't the format date of the Rails view being properly displayed with the initializer?

ruby-on-rails,ruby,ruby-on-rails-4,erb,view-helpers
On my view view, I have the following format I want to output: <tr> <th>Released:</th> <td> <%= movie.release_at.to_s(:release_date) %> (<%= time_ago_in_words(movie.release_at) %>) </td> </tr> The exact format I want is "Month Day, Year", so I created an initialize file with the following: Date::DATE_FORMATS[:default] = "%B %e, %Y" My database schema...

Read value from an array in rb to an erb file

ruby-on-rails,ruby,arrays,erb
I have a global array in Ruby having some string objects. Something like: $my_array = ['apple', 'orange', 'cherries'] In every ERB file, I wish to read one object out of this array. So in my ERB file, I used: "<%= $my_array[0] %>" and this doesn't work. The error I get...

How do I update my view template once my form is submitted via Ajax in RAILS 4?

jquery,ruby-on-rails,erb
I am trying to wrap my head around ajax forms in rails and using Remote => True on my forms. Now this works, When I click on Add New Subject. The following will happen 1.form partial will be appended to the body(this has the form in it) 2.Bootstrap Modal will...

Rails - select_tag form helper is not working properly

ruby-on-rails,ruby,erb
I just recently started working in rails forms, and though I have scoured the api docs many times, I have not been able to figure out how to use it properly. Basically, in my app I have a form to place a manual order (shipping). I would like there to...

Rails removes port from local environment variable?

javascript,ruby-on-rails,port,erb
I have a very strange problem with my rails 4.1.0 application. Inside local_env.yml I have a variable declared as: API_URL: 'http://api.myapp.com:3000' I use this variable in the javascript file app_ready.js.erb like so: var apiHost = '<%= ENV["API_URL"] %>' But for some reason when I call the page using this script...

Rails missing template error, only intermittently

ruby-on-rails,erb
Production website on Rails 4.2.1 Everything fine, but occasionally get strange error on the home page: Missing template home/index, application/index with {:locale=>[:en], :formats=>["text/html;text/plain"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: [...] /releases/20150619150924/app/views" [...] shared/bundle/ruby/2.1.0/gems/devise-3.5.1/app/views" Obviously app/views/home/index.html.erb exists and works fine most of time, but it seems to miss...

Elegant way to only show records if they exist in rails ERB

ruby-on-rails,ruby,erb
I'm finding that I'm using a lot of repetitive code in my ERB views when displaying information about a user. Along the lines of <% if @user.name.present? %> <%= @user.name %> <% end %> I'm basically doing this for everything in my view to avoid errors (e.g. older users who...

Previewing the ruby elements in html.erb with Aptana

ruby-on-rails,aptana,erb
I'm making my first rails app with Aptana studios and I'm trying to preview some of the basic partials I'm making to add to the other views. The problem I have is when I run them using Firefox/IE the ruby elements of the file get printed to screen as is...

Emulation for ERB extention -%> for the purpose of unittests

ruby,rake,chef,erb
I need to evaluate an ERB template, and then ensure it's a valid NGINX configuration file, but vanilla ERB doesn't allow the -%> directive. How am I able to add that extension into my rakefile? I've been able to replicate the problem in irb as so: ~ $ irb irb(main):001:0>...

Linking a link from ERB to the control file

ruby,erb
I'm trying to make a sort part to my ruby erb list, so far it makes the list then i want a way to sort the list when the click "sort by date". I have no idea where to start? I'm sure whether to use the href tag or what...

rails lists my entire database table, again, after already been listed as planned in my divs [duplicate]

ruby-on-rails,erb
This question already has an answer here: Ruby on Rails View Rendering DB Info On Page 2 answers My view re-lists all of my database table contents outside of the panel div where I intended to list them. The HTML <div class="row"> <div class="small-12 large-12 columns"><h2>All recipes</h2></div> <%= @recipes.each...

How do I make sure each post in the Post feed shown in the home page has all its comments displayed under it?

ruby-on-rails,relationship,erb,foreign-key-relationship,instance-variables
I have a Post model, and a Comment model that belongs to Post. I was able to display the Post in the home view corresponding to the home controller and home action and the User/show view. Thus, in the home and user views, the posts are listed in order of...

syntax error, unexpected kENSURE js.erb rails

javascript,ruby-on-rails,ruby,erb
I have a litle problem with my view create.js.erb in my rails project. I want to execute 2 differents block of code if there's a balise with the id JTBDS or not. Here's my code : if( $("#JTBDS").length ){ $("#T<%= @current_tweet %>").after("<%= escape_javascript(render(:partial => @jtbd) %>")); $("#E<%= @current_tweet %>").hide(); $(".jtbd_tags").each(function(){...

Ruby on Rails : How to find a matching value in JSON arrays?

arrays,ruby,json,ruby-on-rails-3,erb
irb(main):001:0> d = OurDomains.find(3) => #<OurDomains:0x000000056ea410 @id="2", @internal_domains=["1.com", "2.com", "3.com"], @external_domains=["2.com"]> Using the above response as an example, could someone give me a guide on how to compare values in the internal_domains and external_domains array please? Specially, I am interested in displaying all the values in internal_domains that corresponds to...

How do I define values in options_for_select?

ruby-on-rails,select,erb,options
I have this options_for_select call: <%= f.select(:agents, options_for_select(@agents), {},{multiple: true, size: 10, :id => "agents"}) %> There are only currently 2 agents in the database but they are showing up as #<Agent:0x007fc1b121c490> format. I want them to display as @agent.name and for the value to be @agent.id. How do I...

gretel gem in rails engine

ruby-on-rails,gem,erb,breadcrumbs
i am working with Rails 4.1.8. Is here someone who worked with gretel in an rails engine before? How do I get breadcrumbs into my rails engine? Can someone post an working example, that would be great, thanks!

Rails: form_for shortening code

ruby-on-rails,ruby,erb,form-for,templating
I am currently learning rails and I have the following code: <%= form_for(@account) do |f| %> <%= f.label :username %> <%= f.text_field :username, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> <%= f.label :password_confirmation, "Confirm...

Setting CSS backgroundImage path in Javascript with ERB

javascript,ruby-on-rails,ruby,erb
I am trying to set the background image of an element using erb in a javascript file document.getElementById("logo").style.backgroundImage = "<%= asset_path('logo.png') %>"; But the image doesn't show. When I inspect the element on the browser and visit the url produced for the image, the image appears. When I change backgroundImage...

Is there a way to add HTML inside a Rails partial argument?

html,ruby-on-rails,erb,partials
I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be. I have a static page in my Rails app which has a lot of...

Rails multiple records select and apply multiple actions

ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,erb
I am quite new at rails and I am having some trouble designing an admin dashboard. What I want to achieve is this: Have a list of multiple users from database. Have the ability to select multiple records. Have the ability to apply different actions to all of the selected...

How do I make a comment form appear and work correctly in my homepage?

ruby-on-rails,forms,erb,instance-variables
I have a Post model, and a Comment model that belongs to Post. I was able to display the Post in the home view corresponding to the home controller and home action and the User/show view. Thus, in the home and user views, the posts are listed in order of...

Add extra options to link_to do

ruby,ruby-on-rails-3,erb
I would like to pass a parameter with this link_to helper. How do I do that? <%= link_to ('/my_controller/my_action') do %> <div>haha</div> <% end %> The original code I had used the standard text link: <%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %> ...

How to render a string attribute as URL in RoR

ruby-on-rails,ruby,erb
Following a RoR turorial where I am building a basic bookmark app. I have two models, topic and bookmark. Bookmarks have a url attribute. The url attribute is rendering fine on my topics#show, but as plain text. When I tried to render it as a link it doesn't link to...

Suppressing spaces in ERB template

ruby-on-rails,erb
Why does this produce the desired output (a list of team names, separated by commas): <% teams.each_with_index do |t,i| -%><%= ',' unless i == 0 -%> <%= link_to t.name, team_path(t) -%> <%- end %> Output: One, Two, Three And this not: <% teams.each_with_index do |t,i| -%> <%= ',' unless i...

Change value of Ruby variable in ERB file upon onlick

ruby,sinatra,erb
I want to change the value of a Ruby local parameter in my .erb file when a user clicks on a bit of text. (I am using Ruby with Sinatra, not Rails). The code looks like this: <div class="modal-body"> <!-- Get ERB to work here --> <p><i>No account? <a href="#"...

Invalid flags supplied to RegExp constructor in js.erb file

javascript,jquery,ruby-on-rails,erb
I'm trying to changes the src attribute of my img(s) when they are hovered over using jQuery. But I am getting an error saying ,Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'icon_history' app.js.erb var hover = <%= image_path 'icon_history-h1' %>; var unhover = <%= image_path 'icon_history' %>; $('img.more').hover(function(){ console.log(hover);...

Add a line break to an erb tooltip?

html,ruby-on-rails,tooltip,erb
I have an erb object with a tooltip: <%= link_to "hover-me", "#", title:"line1<br>line2"%> but the tooltip just displays like this: line1<br>line1 How can I add a linebreak to an erb-generated tooltip?...