Liquid Templates Not Parsing! - ruby-on-rails

Im trying to use Liquid Template Language in my Rails Application, i've watched Ryan Bates' video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work!
When I try something like
#template = Liquid::Template.parse("Hi {{name}}")
#template.render('name' => 'toby')
I get
hi toby
but when i try something like
category = Category.first
#template = Liquid::Template.parse("Hi {{category.name}}")
#template.render('category' => category)
I don't get the desired result, I get only
hi ""
Can someone please help me out with this?

When the value is not a hash, you need to tell liquid which methods it can read from the passed object.
This documentation page show you how to instruct ActiveRecord.
The quickest way is to use the liquid_methods macro.

Related

Ruby on Rails 4 Parsing first 3 RSS items

I'm currently making a website in RoR 4 and I want to include RSS from my blog on the front page. However, currently I get all of the articles from the blog while I need only first 3.
I'm fairly new to rails and I couldn't find any pointers to how I could do such thing.
My current code in the controller is:
def index
require 'rss'
#rss = RSS::Parser.parse(open('FEED_URL').read, false)
end
I imagine I could simply cut the #rss after the third element after it has been parsed, but that seems to me a bit dirty in Ruby. Is there a better way to do it?
Thank you very much!
Looking here, I'd take a gander and try...
#rss = RSS::Parser.parse(open('FEED_URL').read, false).items[0..2]
Feedjira is a fantastic RSS parser. Just do:
Feedjira::Feed.fetch_and_parse('FEED_URL').entries[0,3]

What is the use of _() in ruby/rails

I am looking at the source code of an program trying to figure out how it does certain things. One thing that caught my eye was the use of things like this in the views"
<%= _("Publish settings") %>
and in the controller:
flash[:notice] = _('Article was successfully created')
When I do _("Test") in the console it just returns "Test" string. So what is the use of the _() syntax. Haven't seen it before and can't find anything on google.
Any info will be appreciated.
It surely looks like some localization feature. As far as I can tell preferred method of doing that in Rails3 is t method, but if someone used external GetText gem for this purpose he might end up in such notation.

Ruby on Rails PDF Stamper / iText

I have done a lot of searching and cannot find a solution for getting PDF-stamper to work in my rails application. From the tutorials it appears that I write a method in the model? I wrote a simple app with two fields: nameLast and nameFirst. All I want to do is write these to a PDF I have that contains fields for user info. Two field happen to be FirstName and LastName so perfect time to use PDF-stamper right? I just want to take user data from the rails application and have then be able to push a button and generate a PDF. Here is the method I have in my model.
def savePDF
pdf = PDF::Stamper.new("sample.pdf")
pdf.text :nameFirst, "Jason"
pdf.text :nameLast, "Yates"
pdf.save_as "my_output.pdf"
end
That was clearly taken from a tutorial that I must not properly understand. I can actually get this working in java pretty easy, but I don't want to use jRuby. I am using rjb which is working fine. I just don't think I properly understand what needs to happen to get this working. Any help is greatly appreciated!
I'm the author of the pdf-stamper gem.
The save_as method saves the created PDF to the filesystem. If you are building a Rails application, I don't think that is what you want.
I'm guessing from your question you want to send a "stamped" PDF back to the browser. If that is the case, you should call to_s on the created PDF and then pass the output of that to Rails send_data method.
In your controller(not the model) you'll want to add some code like this.
def send
pdf = PDF::Stamper.new("sample.pdf")
pdf.text :nameFirst, "Jason"
pdf.text :nameLast, "Yates"
send_data(pdf.to_s, :filename => "output.pdf", :type => "application/pdf",:disposition => "inline")
end
The problem here really is the documentation for the pdf-stamper gem. The feature you wanted was there just undocumented, hence your confusion. I'll have to fix that.
i was doing the same with use of xfdf as a source data for fields, the following code worked for me, maybe it will be helpful to you aswell:
pdfreader = Rjb::import('com.itextpdf.text.pdf.PdfReader')
pdfstamper = Rjb::import('com.itextpdf.text.pdf.PdfStamper')
pdffields = Rjb::import('com.itextpdf.text.pdf.AcroFields')
xfdfreader = Rjb::import('com.itextpdf.text.pdf.XfdfReader')
pdf = pdfreader.new("#{Rails.root}/public/out/temp/form1.pdf", nil)
xfdf = xfdfreader.new(f)
stamp = pdfstamper.new(pdf, filestream.new("/tmp/text#{i}.pdf"))
pdffields = stamp.getAcroFields()
pdffields.setFields(xfdf)
stamp.close

How should I write scraper that searched for a constant button targeted at one domain

I would like to scrape an entire domain ex(Tumblr.com) and search each blog for an embedded tag. Can this be done with rails?
This is not a simple question to answer.
I would point you to:
http://stdlib.rubyonrails.org/libdoc/open-uri/rdoc/index.html
sure, if this tag has an css selector (id, class) or a specific XPATH that you can search for, you should use Mechanize a powerful ruby library.
Something like
agent.page.search(".mytag").each do |item|
day = item.at("a").text
item.search("p").each do |e|
image = e.at("a")
agent.get(image).save_as("images/img#{rand(1000)}_#{File.basename image}")
end
end

Built-in way to convert xml to an ActiveRecord Object in Rails 3?

In Rails 3, is there a way to produce an ActiveRecord object from xml in a controller without writing code yourself to explicitly parse it? Say for example, could a controller receive xml like
<user>
<first_name>Bob</first_name>
<last_name>Smith</last_name>
</user>
and have it produce a proper User object similar to User.new(params[:user])? This is for an api.
Yes, you can do it like this:
#user = User.new
#user.from_xml(xml_data)
Update
On overriding you can do something like this:
#user.rb
def from_xml(xml_data)
book = Book.new
book.from_xml(extract_xml_from(xml_data))
self.books << book
super(xml_data)
save
book.save
end
Please note that the most important line in the overriding is the super(xml_data) which will take care on calling the original from_xml(xml_data) of the ActiveRecord model.
So you can customize the rest as needed, but this line is neede if you want to get the original functionality as well.
Let me know if something is not clear.
I've created a gem, xml_active that might help you with this without having to write a lot of code. You can check it out at https://rubygems.org/gems/xml_active.
To get it to create one object with associations just do the following:
book = Book.one_from_xml xml_data
You can also get xml_active to create many objects from xml along with associations. There are more features but probably not in the scope of this answer. You can check them out on the home page for the gem.
UPDATE
xml_active has now been officially retired and development is now focused on data_active (see https://github.com/michael-harrison/data_active) which has the functionality of xml_active but in future releases I will be working to support other formats

Resources