I am trying to simply add an HTML class if a database value returned is a specific number. Here is what I tried:
<ul class="dog-sizes-list">
<li <%= if given_size == 1 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="30px" /></li>
<li <%= if given_size == 2 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="40px" /></li>
<li <%= if given_size == 3 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="50px" /></li>
<li <%= if given_size == 4 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="60px" /></li>
</ul>
However nothing happens and no error is produced. Furthermore, I ran the AR code in console and it returned the correct value. Specifically:
given_size = Client.where(user_id: listing.client_id).first.dogsize
What is the best approach for this?
You could get the required result in this manner:
<li class="<%= "active" if given_size == 1 %>"></li>
If you are using this kind of method regularly it would be adivsable to extract it into a helper method. The helper can be placed into your application_helper.rb file or other suitable place
def class_for_thing(thing)
if thing == whatever
"blah"
else
"not-blah"
end
There is no point of "active" condition because all items are active according to your code.
It seems you only want to customize the image size according to the given size. If so the better way is to define it in model, or better, in a presenter.
class Dog < ActiveRecord::Base
SIZE_AND_IMAGE = { '1': '30px', '2': '40px', '3': '50px', '4': '60px' }
# Can fallback to a default size of 30px if no size given
def image_size
given_size ? SIZE_AND_IMAGE[given_size.to_s] : '30px'
end
end
# In View
<ul class="dog-sizes-list">
<li><img src="/assets/dogs/dog-icon.png" width="#{#dog.image_size}" /></li>
Related
In an ng-repeat I want to add a hide-xs attribute on an element based on the current scope.
How can I do that?
I basically want to do something like (this obviously don't work):
<li ng-repeat="item in items" hide-xs="{{ item.showAlways ? 'false' : 'true' }}">
{{item.title}}
</li>
Edit
I ended up doing this (as suggested by DieuNQ) but if anybody know how to do it using directive and not class I would take it
<li ng-repeat="item in items" ng-class="{'hide-xs': !item.showAlways }}">
{{item.title}}
</li>
hide-xs not works like that (it does not depend on true or false). It just add class to your tag. Try this:
In your html:
<li ng-repeat="item in items" ng-model="item.showAlways" ng-class="someClass">
...
</li>
In your controller:
$scope.item.showAlways ? $scope.someClass == '' : $scope.someClass == 'hide-xs';
I want to separate this block with commas :
- game_publication.groups.each_with_index do |group, index|
= link_to store_group_path(current_store, group) do
%span= #groups.find(group).name.to_s + (index > 0 ? ', ' : '')
But for the moment it returns something like
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test,</span>
</a>
</label>
It doesn't seems a situation where I can use any rails helpers.
I would like something like group1, group2, group3.
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard,</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test</span>
</a>
</label>
First, are you sure that you pasted here the exact code which gave the posted result? In your code, you have
(index > 0 ? '' : ',')
which means: Do not add a comma UNLESS we are on the first element. The result you posted had the comma the otherway round: It has a comma everywhere, EXCEPT for the first element. With other words: The code you posted, can't produce the output you posted.
Now for your problem: You want to add a comma on every element, except the last. This means that you need to know the highest (last) index value:
last_index = game_publication.groups.size - 1
With this, you can write your expression as
(index == last_index ? '' : ',')
I have an html template that filters a list by the column property of the objects of that list like so:
<ul>
<li card-view
card-id="state.card"
ng-repeat="state in ctrl.game.states | filter:{column:'backlog'} "
ng-include="cardview.html">
</li>
</ul>
If I modify the column property in one of the elements of that list, the display does not update.
How can I make that happen?
Here's one option that uses an imaginary placeholder tag and avoids the |filter replacing it with an ng-if, but I hope someone has a better answer than this one.
<ul>
<xx ng-repeat="state in ctrl.game.states">
<li card-view
card-id="state.card"
ng-if="state.column == 'backlog'"
ng-include="cardview.html">
</li>
</xx>
</ul>
Doing the ng-if and ng-repeat on the same element didn't work.
I am using the feed_parser gem in a Rails app. It works exactly as indicated in development and also works in the production console, but it won't load on the production web server.
(Note: It even works fine on our Ubuntu 12.04 test server, fails on 10.04 production server)
uninitialized constant - Project::FeedParser
I am running this in one of the models of our website:
class Project < ActiveRecord::Base
def self.facebook_feed
url = "http://www.facebook.com/feeds/page.php?id=236004913152511&format=rss20"
posts = Project.parse_feed(url)
return posts
end
def self.blogspot_feed
url = "http://fundinggarage.blogspot.com/feeds/posts/default?alt=rss"
posts = Project.parse_feed(url)
return posts
end
private
def self.parse_feed(feed_url)
feed = FeedParser.new(:url => feed_url).parse
fj = feed.as_json
#fj[:items].first[:description]
posts = []
fj[:items].take(4).each do |fp|
post = {}
doc = Nokogiri::HTML(fp[:description])
img_srcs = doc.css('img').map{ |i| i['src'] }
post[:headline] = fp[:title]
post[:image] = "/assets/fg_image_placehloder.png"
post[:image] = img_srcs.first unless img_srcs.first.nil?
post[:url] = fp[:link]
post[:date] = fp[:published]
posts << post
end
return posts
end
end
In the view:
<% Project.blogspot_feed.each do |fb| %>
<div class="grid_3">
<div class="media other-post-item">
<a href="<%= fb[:url] %>" class="thumb-left" target="_blank">
<div class="blog-img">
<img src="<%= fb[:image] %>" alt="<%= raw fb[:headline] %>" title="<%= raw fb[:headline] %>">
</div>
<span class="be-fc-orange">
<h4 class="rs title-other-post"><%= raw fb[:headline] %></h4>
<p class="rs fc-gray time-post pb10"><%= "#{time_ago_in_words(fb[:date])} ago" %></p>
</span>
</a>
</div>
</div><!--end: . other-post-item -->
<% end %>
The interpreter is looking for the FeedParser class to be defined, and can't find it - the error indicates it is looking in the Project class, as it can't find it elsewhere.
I would add the require 'feed_parser' to the top of the Project model, above the class declaration.
I am assuming this is the gem you are using:
https://rubygems.org/gems/feed_parser
I have the following HTML structure
<div id='my_categories'>
<ul>
<li>Animals, Birds, & Pets</li>
<li>Ask the Expert
<ul>
<li><a href='21'>Health Care Providers</a></li>
<li><a href='22'>Influnza</a>
<ul>
<li><a href='221'>Flu Viruses (2)</a></li>
<li><a href='222'>Test</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
This is how the web page looks
What I need is, I have a categories table with fields category_name, category_url and parent_id.
I need to save each category and sub-category. The parent_id denotes under which category does this sub-category comes under.
How can I parse through this HTML structure using this Hpricot and save data to my database. Please help
My table looks like
id category_name category_url Parent_id
1 Animals, Birds, & Pets null null
2 Ask the expert null null
3 Health Care Providers null 2
4 Influenza null 2
5 Flu Viruses null 4
6 Test null 4
Thanks in advance
Below is the code that worked for me...
doc = Hpricot(open(categories_page).read)
doc.search("ul/li").each do |li|
category = li.search('a[#href]').first.inner_text.gsub(/ *\(.*?\)/, '')
category_url = li.search('a').first[:href]
category = Category.find_or_create_by_name(category, :url => category_url)
puts "---------- #{category.name} ------------"
nodes = li.search("ul/li/a")
unless nodes.empty?
nodes.each do |node|
node_name = node.inner_text.gsub(/ *\(.*?\)/, '')
node_url = node.attributes['href']
sub_category = Category.find_by_name(node_name)
if sub_category.blank?
sub_category = Category.create(:name => node_name, :url => node_url, :parent_category_id => category.id)
puts " #{sub_category.name}"
else
sub_category.update_attribute('parent_category_id', category.id)
puts " #{category.name} --> #{sub_category.name}"
end
end
end
end