Very nice screencast, Ryan! I had the same problem with ActiveRecord on rails 2 which requires some hacks in order to make the model not db specific. Great work on introducing ActiveModel! You mentioned downloading the rails 3 codes and read it. I just found out a neat tool that makes it easier to read codes from a source control system on your browser, it's called CodeFaces, here is the restful link to read the rails code on GitHub: http://codefaces.org/http://github.com/rails/rails
I wish you would do a screencast about how to add multiple records of a single model with one form. I've done a ton of searching and can't seem to find a satisfactory answer that doesn't involve using a parent class.
One note: instead of #pdf_link, maybe you could have used .no_print. It seems likely that there will be other items you don't want to print - using an id tag for that one seems pretty heavy.
first off, great video. i'm learning so much about rails through these screencasts so i really appreciate your work ryan.
i had a question: (not sure if anyone has faced this issue but) i'm getting an error when attempting to use my UserMailer in the users controller. the error i get is:
I really wish I had this to hand a couple of weeks ago when trying to figure out the subdomain stuff in Rails 3. This has helped me to tidy up my code somewhat.
I'd really like to see the ":subdomain" option on url helpers as a patch for Rails 3, will you be submitting? ...If not I may do so as it's super useful.
I noticed a lot of people running into a problem where their nested records were not saving to the data base.
There seems to be an issue with what Rails version is being used. But if you are using 2.3.5 or higher your code should work just as Ryan says.
I made a stupid mistake that had me stumped for hours, so I thought I would make a note of it in case anyone else is having the same problem.
The mistake I made was a typo in the accepts_nested_attributes_for in my model. You should double check the symbol that you are checking if '.blank?' or not.
If you have a typo, then of course it will be 'blank?' and therefore will always be rejected and never saved.
There's a blog article on my.opera.com that describes how the Opera browser deals with cookie sharing between subdomains.
To summarize (very quickly) the article: any *.example.tld subdomains are considered as siblings (and can share domain=.example.tld cookies) if the example.tld domain has a registered IP address in DNS.
Hi, Ryan.
I've wrote similar article some months ago (http://bit.ly/ajGym2 - sorry, only Russian version, but you can use Google Translate) but there are some problems with session sharing between subdomains in Opera browser - unfortunately Opera cannot read cookies from sibling domain - it means if you set cookie for dom1.example.com cookie cannot be read from dom2.example.com
Just following up on my own comment above. I got the nested_form plugin to work. Posted up my working example and some notes on what I ran into here: http://gist.github.com/465023
For anyone interested, you should check out Ryan's nested_form plugin as it basically implements exactly what he covers in this screen cast but is a cleaner implementation.
Finally got Rails 3 working with RVM. Stopped using my local Passenger installation and now I'm really enjoying the flexibility of jumping around to different versions of Ruby/Rails.
Currently developing an app that requires account level subdomains and was unhappy with the options in Rails 2.3...
Looking forward to Rails 3 RC, thanks again for all the awesome screencasts!
I've been trying to get Devise and subdomains to work together, by using one of the Subdomain plugins, but not managed to get there fully. Going to have a go using your approach now and see how I get on.
Keep up the great casts, they are always mega helpful!
I just got this to work in Rails 2.3.8 and Ruby 1.9 using the updated code on this page, but I had to change one thing, and I wanted to share my solution. I had to add a 'project_id' column to my tasks table. Until I added this column, I could create a new Project, but not edit it, and this was the reason. If you're getting an error saying 'no such column... task.project_id' then try this. Thanks for the amazing work Ryan!
I have the same problem as @Walter with the password email not working with smtp. Unfortunately, I can't set my environment to use sendmail. Has anyone gotten this to work with smtp? I get "A sender (Return-Path, Sender or From) required to send a message". Other than that, it's working great in Rails 3!
This didn't work with the non-standard rails3-generators for me, but putting the line
require 'rails/generators'
into a file (named anything) within config/initializers fixed it. I get errors when it tries to created the helpers and stylesheets for some reason, though (error shoulda [not found] it says, though it finds it for the test).
Works fine in irb console but in the real app I get the following error:
Processing PostsController#show to pdf (for 127.0.0.1 at 2010-06-30 22:44:01) [GET]
Parameters: {"id"=>"1"}
Post Load (0.4ms) SELECT * FROM "posts" WHERE ("posts"."id" = 1)
Completed in 13ms (View: 4, DB: 0) | 406 Not Acceptable [http://localhost/posts/1.pdf]
pdfKiT works great for me but only on Rails 3
in Rails 2.3.8 if I do ".pdf" on url it returns empty pdf with "nil". Anybody had that problem ?
Creating from command line works without any problems.
Got to love PDFs and this should be very powerful. Can't wait to deploy in my app. Just one thing though, sems very straightforward, I followed the steps, in a test 2.3.8 app with a simple scaffold.
Can't get any text to display on the PDF itself, a PDF renders from the show action /show/1.pdf : but it just contains the word "nil" rather than displaying the record as the html view does.
@dhc (Since people are asking): You could define an "ActiveModel::Base" class and include the functionality you want for your app. The nice thing about the modular architecture is that you can do exactly what you are looking to do, but it's not forced upon you. So just define your own base class and include the modules you want for all of your inheriting classes. Don't get this confused with ActiveRecord, as that a different set of classes and modules, that happen to leverage the ActiveModel classes/modules.
Found out that page-break-before only works if you have a really simple layout, with very few nestings.
Now I only wish "page-break-inside: avoid;" would work...
I will very soon be working on wicked_pdf with the intention of bringing it up to date on Rails 3, and Ruby 1.9.2. I might be looking at relying upon PDFKit, if I can do so without losing any of the flexibility of wicked_pdf. I think that will ultimately be better for both projects.
"Curious to know if anybody has tried creating a table including images?"
it should work for *any* html, as it uses webkit (the renderer from chrome, safari etc.) to 'display' the html page internally, then draws this into a pdf, in the same way you can export a pdf directly from any program on os x by clicking the PDF button in the print dialog.
"How possible is it to have Rails automatically save a copy of this pdf on the server? I need a confirmation sheet for our folks in the office to use and I'd prefer to not have to do them one at a time if possible."
you could probably create a controller action which uses pdfkit or similar to save a pdf directly to an arbitrary filesystem path. look through its api for a method which returns a File or IO object, it likely uses one internally anyway.
Great screencast, once again you were providential:
Just when I was thinking I had to learn Prawn I opened my RSS reader and there was your fresh new tutorial!
Thanks!
Very nice screencast, Ryan! I had the same problem with ActiveRecord on rails 2 which requires some hacks in order to make the model not db specific. Great work on introducing ActiveModel! You mentioned downloading the rails 3 codes and read it. I just found out a neat tool that makes it easier to read codes from a source control system on your browser, it's called CodeFaces, here is the restful link to read the rails code on GitHub: http://codefaces.org/http://github.com/rails/rails
I wish you would do a screencast about how to add multiple records of a single model with one form. I've done a ton of searching and can't seem to find a satisfactory answer that doesn't involve using a parent class.
Great episode; I'll have to check pdfkit out.
One note: instead of #pdf_link, maybe you could have used .no_print. It seems likely that there will be other items you don't want to print - using an id tag for that one seems pretty heavy.
But that's a nit! Thank again.
Hi Ryan,
I was desperately looking for a working example of PDFKIT.
This is great!!!!
Thanks a lot!!!!!
first off, great video. i'm learning so much about rails through these screencasts so i really appreciate your work ryan.
i had a question: (not sure if anyone has faced this issue but) i'm getting an error when attempting to use my UserMailer in the users controller. the error i get is:
"uninitialized constant UsersController::UserMailer"
has anyone had this problem?
@Alexander, I mention changing the top level domain size at the end of the episode. I haven't tested it, but passing "0" for the tld size should work.
@Jamie, I think the problem with adding a built-in :subdomain option is that the top level domain size is not easily configurable.
That said, I think a ticket on configuring the default tld size along with this would be great. I don't have plans to do this so feel free to.
Thanks Ryan!
I really wish I had this to hand a couple of weeks ago when trying to figure out the subdomain stuff in Rails 3. This has helped me to tidy up my code somewhat.
I'd really like to see the ":subdomain" option on url helpers as a patch for Rails 3, will you be submitting? ...If not I may do so as it's super useful.
Thanks again.
Hello Ryan,
thank you for another great episode. You can actually obtain the url text using css:
http://gist.github.com/465259
Of course, it's better to put that into a print.css stylesheet. I am not sure if it works in IE6, BTW.
I noticed a lot of people running into a problem where their nested records were not saving to the data base.
There seems to be an issue with what Rails version is being used. But if you are using 2.3.5 or higher your code should work just as Ryan says.
I made a stupid mistake that had me stumped for hours, so I thought I would make a note of it in case anyone else is having the same problem.
The mistake I made was a typo in the accepts_nested_attributes_for in my model. You should double check the symbol that you are checking if '.blank?' or not.
If you have a typo, then of course it will be 'blank?' and therefore will always be rejected and never saved.
Example below of typo:
accepts_nested_attributes_for :links,
:reject_if => lambda { |a| a[:typo].blank? },
:allow_destroy => true
@Alexey Poimtsev:
There's a blog article on my.opera.com that describes how the Opera browser deals with cookie sharing between subdomains.
To summarize (very quickly) the article: any *.example.tld subdomains are considered as siblings (and can share domain=.example.tld cookies) if the example.tld domain has a registered IP address in DNS.
The reality is a little more complicated, so the
http://my.opera.com/yngve/blog/show.dml/267415 article deserves a read.
To solve the problem with Rails 2.3++ look at http://github.com/perenecabuto/PDFKit/commit/c61f8d683c2894c2cfecf08b43707c63514c4c9f it works for me.
Hi, Ryan.
I've wrote similar article some months ago (http://bit.ly/ajGym2 - sorry, only Russian version, but you can use Google Translate) but there are some problems with session sharing between subdomains in Opera browser - unfortunately Opera cannot read cookies from sibling domain - it means if you set cookie for dom1.example.com cookie cannot be read from dom2.example.com
Just following up on my own comment above. I got the nested_form plugin to work. Posted up my working example and some notes on what I ran into here: http://gist.github.com/465023
For anyone interested, you should check out Ryan's nested_form plugin as it basically implements exactly what he covers in this screen cast but is a cleaner implementation.
Probably not the right place to ask, but I really liked the nested_form plugin that Ryan mentions.
However, the remove task link is not working, and when I add a task it adds it to the top of the page.
It seems I have something basic that is wrong.
This is the code that is not working:
http://gist.github.com/464979
I would appreciate it if anyone could point me in the right direction.
@dhc: It removes the passed element from the hash and returns it. See http://ruby-doc.org/core/classes/Hash.html#M002870
Is it possible to use domain size 0? I need to show different content depending on domain name, but the main part of the site is the same.
In this line of your code:
options[:host] = with_subdomain(options.delete(:subdomain))
what does options.delete() do? It doesn't seem intuitive, which is rare in Ruby.
--dhc
Great vid, was meaning to have a look at the subdomains in rails3, but you just summed it up nicely.
Also plus points for picking up on smackaho.st :)
Finally got Rails 3 working with RVM. Stopped using my local Passenger installation and now I'm really enjoying the flexibility of jumping around to different versions of Ruby/Rails.
Currently developing an app that requires account level subdomains and was unhappy with the options in Rails 2.3...
Looking forward to Rails 3 RC, thanks again for all the awesome screencasts!
yum yum *gobbles*.
Thanks for this Ryan!
I've been trying to get Devise and subdomains to work together, by using one of the Subdomain plugins, but not managed to get there fully. Going to have a go using your approach now and see how I get on.
Keep up the great casts, they are always mega helpful!
Is it possible to use searchlogic in rails 3? If not, what's the best other solution? Thanks
Any body have any 2 cents on when is a good time to use Factory.stub? Just curious. Thx.
I just got this to work in Rails 2.3.8 and Ruby 1.9 using the updated code on this page, but I had to change one thing, and I wanted to share my solution. I had to add a 'project_id' column to my tasks table. Until I added this column, I could create a new Project, but not edit it, and this was the reason. If you're getting an error saying 'no such column... task.project_id' then try this. Thanks for the amazing work Ryan!
Now God supports sending notifications to an iPhone via Prowl using the Prowly gem (http://github.com/rafmagana/prowly)
Hi, Great screencast.
having the same problem that 26 and 66
Can anyone give a hand?
I am having the same error that marek(comment 26).
Environment:
Rails 2.3.8 con bundle.
wkhtmltopdf in '/usr/local/bin'
And the error:
406 Not Acceptable [http://localhost/whatever.pdf] and no output.
Any ideas?
Thank you
Is there any easy way to validate the file type or file size ONLY if the form is being submitted with an attachment?
Thanks for this. Awesome alternatives to the expensive princeXML.
I have the same problem as @Walter with the password email not working with smtp. Unfortunately, I can't set my environment to use sendmail. Has anyone gotten this to work with smtp? I get "A sender (Return-Path, Sender or From) required to send a message". Other than that, it's working great in Rails 3!
This didn't work with the non-standard rails3-generators for me, but putting the line
require 'rails/generators'
into a file (named anything) within config/initializers fixed it. I get errors when it tries to created the helpers and stylesheets for some reason, though (error shoulda [not found] it says, though it finds it for the test).
With regards to the return_url, Shouldn't there be a parameter (eg. session_id) to reconnect to the same session?
Works fine in irb console but in the real app I get the following error:
Processing PostsController#show to pdf (for 127.0.0.1 at 2010-06-30 22:44:01) [GET]
Parameters: {"id"=>"1"}
Post Load (0.4ms) SELECT * FROM "posts" WHERE ("posts"."id" = 1)
Completed in 13ms (View: 4, DB: 0) | 406 Not Acceptable [http://localhost/posts/1.pdf]
Anybody know who to get this to work?
Any help would be greatly appreciated J.
Great stuff. I really enjoyed reading this article. I will come back to check it out for update.
Thank you for a great cast.
pdfKiT works great for me but only on Rails 3
in Rails 2.3.8 if I do ".pdf" on url it returns empty pdf with "nil". Anybody had that problem ?
Creating from command line works without any problems.
Many thanks.
Same problem as Rupert Fisher... I obtain a 'nil' blank page every time :(
Anyone knows how to display the current page number and the total number of pages?
page-break-avoid does not yet work in wkhtmltopdf. it's because this would require heavy changes in webkits rendering ...
If page-breake-before does not work. Try doing "clear: both; page-break-before: always; display: block;" ...
The clearing should do the trick.
Got to love PDFs and this should be very powerful. Can't wait to deploy in my app. Just one thing though, sems very straightforward, I followed the steps, in a test 2.3.8 app with a simple scaffold.
Can't get any text to display on the PDF itself, a PDF renders from the show action /show/1.pdf : but it just contains the word "nil" rather than displaying the record as the html view does.
Sorry if I'm being dense, any ideas?
@dhc (Since people are asking): You could define an "ActiveModel::Base" class and include the functionality you want for your app. The nice thing about the modular architecture is that you can do exactly what you are looking to do, but it's not forced upon you. So just define your own base class and include the modules you want for all of your inheriting classes. Don't get this confused with ActiveRecord, as that a different set of classes and modules, that happen to leverage the ActiveModel classes/modules.
Found out that page-break-before only works if you have a really simple layout, with very few nestings.
Now I only wish "page-break-inside: avoid;" would work...
Thanks for plug, Mr. Bates!
I will very soon be working on wicked_pdf with the intention of bringing it up to date on Rails 3, and Ruby 1.9.2. I might be looking at relying upon PDFKit, if I can do so without losing any of the flexibility of wicked_pdf. I think that will ultimately be better for both projects.
Hi again Ryan,
Can't really make page-break-before work, it's always cutting my tables...
What version of wkhtmltopdf are you using?
actually if you only need to do it once/rarely a rake task would be better than mucking up your controllers and routing
"Curious to know if anybody has tried creating a table including images?"
it should work for *any* html, as it uses webkit (the renderer from chrome, safari etc.) to 'display' the html page internally, then draws this into a pdf, in the same way you can export a pdf directly from any program on os x by clicking the PDF button in the print dialog.
"How possible is it to have Rails automatically save a copy of this pdf on the server? I need a confirmation sheet for our folks in the office to use and I'd prefer to not have to do them one at a time if possible."
you could probably create a controller action which uses pdfkit or similar to save a pdf directly to an arbitrary filesystem path. look through its api for a method which returns a File or IO object, it likely uses one internally anyway.
Thanks Ryan. You set the bar.
Great screencast, once again you were providential:
Just when I was thinking I had to learn Prawn I opened my RSS reader and there was your fresh new tutorial!
Thanks!
really really cool screencast and very nice thing this pdfkit :)
Prawn for clean structures
PDFKit for fancy looks ;)
thanksss you very very much
asdasasdasd