I basically want to do this:
<% #videos.each do |vid| %>
<div id=vid.location>
...
<% end %>
how do I evaluate vid.locaion and use it as the id attribute?
i've tried the above, id="#{vid.location}" and id="<% vid.location %>" (the last one with and without quotes.
any help appreciated
Easy,
<div id="<%= vid.location %>">
Your first attempt was wrong as you're still in markup - not ruby. In the last one you used <% rather than <%=, so while it was evaluating the getter, it just didn't present it to your view.
Related
I'm running Rails v4.x and Ruby v2.3. I'd like to use a debugging tool (e.g., debugger) to dynamically test different execution paths in a Rails view. Specifically I'm trying to test the two paths of an if statement (ie., "if" block and "else" block) within a Rails view. Here is the code withing the View I'd like to test:
<% if #categories.any? %>
<% #categories.each do |category| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li class="article-title">
<%= link_to "#{category.name}", category_path(category) %>
<li><small><%= pluralize(category.articles.count, "article") %></small></li>
</li>
</div>
</div>
</ul>
<% end %>
<% else %>
<div class="center">
<p>There are no categories currently defined at this time</p>
</div>
<% end %>
To do this I'd like to be able to dynamically change the return value of the #categories.any? method so as to force the desired execution path. Is there a tool that I could use to do this? Can I do with "debugger"? Pry?. If so could you provide some simple instructions of how it could be done?
NOTE: #categories is an instant variable containing values from a data model (Category). I'd prefer not delete the contents of the Category table just to test the else part of the aforementioned code.
Regards,
Jet
Well, as you will have to edit the file anyway, to add a debugger line, you can just as well add another variable, can't you?
<% if (#categories.any? && !#never_run) || #always_run %>
I'm learning Ruby and Rails, and the tutorials have been saying I should set everything up in the controller first for what my ERB file is going to need. I'm having some issues doing that.
I'm getting a product catalog from an API and I'm getting back an array. Here's the start of it:
[{"Name"=>"3x4 Vinyl Magnet", "Description"=>"Made of durable high-gloss vinyl. Measures 3x4 inches and has rounded corners. Waterproof and scratch resistant.", "UnitPrice"=>8.99, "ProductCode"=>"Gift65001"
There are more multiple products.
In my controller file I have:
#http_party_json = JSON.parse(#response.body)
#http_party_json.each do |i|
i.each do |x|
#just_products = x['Products']
end
end
#just_products.each do |grab|
#product_code = grab['ProductCode']; #product_code.delete! ';'
#just_names = grab['Name']
#just_prices = grab['UnitPrice']
#just_descriptions = grab['Description']
end
My ERB file is:
<div class="large-12 columns">
<div class="row">
<% #just_products.each do %>
<div class="large-3 columns panel radius" style="height: 600px"><h2><%= #just_names %></h2><br><h3><%= #just_prices %></h3><br>
<p><%= #just_descriptions %></p>
<button id="<%= #product_code %>">Add to Cart</button>
</div>
<% end %>
</div>
It's displaying the last item in the array for each result it's bringing back. If I transfer the grab loop from the controller to the ERB file, it works fine, but I want to learn how to do it correctly.
I should set everything up in the controller first for what my erb file is going to need
I see the trouble... it's a question of interpretation of what the book is trying to say. In your code, this block,
#just_products.each do |grab|
and all the assignments within it is actually not doing anything for you. After it's done spinning through #just_products.each, the final assignment it makes to #just_descriptions is merely the last value of 'Description' in #just_products, which is what you're seeing in your view.
The book merely says to prepare everything that the erb view is going to need. This does not translate to, "assign every possible variable explicitly in the controller". It is perfectly acceptable to pass #just_products to your view and spin through it (via .each) in there.
If you prefer not to do this, your alternate option would be to build arrays of codes, names, descriptions, etc. in the controller. Then spin through each of these in your view. In this case, this line:
#just_descriptions = grab['Description']
would look more like:
#just_descriptions << grab['Description']
There reason it's showing the last one is because you are looping through the array and overwriting the ivar every time. So when you get to the last item in the array you have overwritten each preceding one.
The main thing you want to avoid doing in your view is making SQL queries. Here, you've already loaded everything into an array and that's enough.
<div class="large-12 columns">
<div class="row">
<% #just_products.each do |product| %>
<div class="large-3 columns panel radius" style="height: 600px"><h2><%= product['Name'] %></h2><br><h3><%= product['UnitPrice'] %></h3><br>
<p><%= product['Description'] %></p>
<button id="<%= product['ProductCode'].delete!(';') %>">Add to Cart</button>
</div>
<% end %>
</div>
I have an article, and each article has tags. Right now I call all the tags together:
<% #articles.each do |article| %>
<div class="articlebox">
<article>
<h4>
<%= link_to article.title, article_path(article) %>
</h4>
<%= markdown article.body %>
<span class="articletagbox">
<%= article.tag_tokens %>
</span>
<% end %>
Right now my class goes around all the article tags. I want it to go around each article tag individually.
I've tried a simple
<% #tags.each do |tag| %>
but that gives me an undefined method "each" nilclass error.
I know this is pretty simple, but I just can't figure out what I'm supposed to change to make it work. I assume I need to define something in my article model?
Thanks!
EDIT
So right now my code looks like this:
<% article.tags.each do |tag| %>
<span class="articletagbox">
<%= article.tag_tokens %>
</span>
<% end %>
and the method in article.rb is:
def tag_tokens
self.tags.collect{|t| t.name}.join(", ")
end
which is returning all the tags associated with each article the same number of times as there are tags.
So for example, if I have three tags on an article: tag1, tag2, tag3 I get
<class>tag1 tag2 tag3</class> <class>tag1 tag2 tag3</class> <class>tag1 tag2 tag3</class>
Instead I want
<class>tag1</class> <class>tag2</class> <class>tag3</class>
So I'm just not sure why I'm getting all the tags associated with each article returned together, but the same number of times as there are tags. I hope that makes sense.
If one article has many :tags then in the article loop do
<% article.tags.each do |tag| %>
Following your edit : you're outputting a method called on the article while looping on tags. Doesn't make sense, you're looping on tags, call tag.name instead!
Check slhck answer for the specific syntax
You said you wanted:
<class>tag1</class> <class>tag2</class> <class>tag3</class>
I'm assuming by class you mean the span formatting that should be applied to each tag individually? Just get rid of the tag_tokens method. It would probably be bad style to define formatting methods in your tag's model anyway – rather do the formatting in a view, or a partial.
In your view, inside the loop where you go over all articles, just do:
<% article.tags.each do |tag| %>
<span class="articletagbox"><%= tag.name %></span>
<% end %>
This would output:
<span class="articletagbox">tag1</span>
<span class="articletagbox">tag2</span>
... and so on.
I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:
<% #Items.each do |i| %>
<%= i.itemname %>
<div class="menu_body">
Link-1
</div>
<% end %>
So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?
Thanks!
You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:
<% #Items.each do |i| %>
<div><%=h i.itemname %></div>
<div class="menu_body">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
You canchange the public/stylesheets/scaffold.css if you want.
Or if you want to change it for a single page say items/index.html.erb
<style>
p{
/* your style here *?
}
</style>
Hey! I iterate through a hash with #lists.each do |list|. I create a div in every cycle that must have an id. I would have created a count variable in PHP to get a definite id. What is the best way to do that in a Rails view? Thank you!
Assuming these are ActiveRecord models (ie. from a database), you could just use the dom_id helper, like so:
<% #lists.each do |list| %>
<div id="<%= dom_id(list) %>">
... rest of list ...
</div>
<% end %>
That way each div will get an ID like list_49, with the number corresponding to the ID in the database.
An alternative would be to use the div_for helper:
<% #lists.each do |list| %>
<% div_for(list) do %>
... content in div ...
<% end %>
<% end %>
That will add the same format of id as Luke's suggestion. Be aware though that it would also add a class attribute of list. You can add additional classes by passing in :class => 'my-class'.
Why don't you just take the id of the list? Or must the id be any kind of "globally unique"?
Update: I think you should go with luke's answer.