Hi! I really enjoy to use prawn + prawnto but have one problem:
My PDF (Rails 2.3.1, Prawn 0.6.3, Prawnto, prawn-core, prawn-format, prawn-layout) is generated from a prawnto-template and gets data from a from which also produces csv, so the parameter format pdf is added in a method which will be called before generating the pdf. The produced PDF has no file extension. How to solve this problem? Tried already respond_to block, but did not help.
def ready_for_mobi
agent = request.headers["HTTP_USER_AGENT"].downcase
MOBILE_BROWSERS.each do |m|
return request.format = :mobile if agent.match(m)
end
end
Thanks for this episode (as well as all the others).
At the beginning of the episode, you touched on the notion of browsing your development app through a mobile device. I've run into a little problem with this that's probably an obvious fix. When I try to hit my local dev app from any machine other than the computer that is running the dev app, none of the assets, such as stylesheets and javascript, load. When I look at the html, I see that "localhost:3000" is embedded in the urls for those assets. What should I change to make this work properly?
My current dev setup is:
- Mac OS X 10.6
- Rail 2.3.5
- start dev app using "script/server" which uses mongrel
What if you always want to start with at least one question? Let's say a user submits a survey with no questions, but then wants to add a question when editing the survey?
I know this can be done via javascript (see part 2), but how would I do this without javascript?
I'm basically looking to get the edit view to populate with at least one question field even if that question doesn't exist, and when update_attributes is called to add the new question that was entered.
At the moment, I can do:
If @survey.questions.empty?
@survey.questions.build
end
In my edit method, but the update method is not saving the new question added when editing a survey.
Hey Ryan,
Big fan of your work! I was trying out the code, but ran into a bit of an issue.
The join table is saving "null","null" as the product_id, category_id. When I have 2 categories selected, I have 2 rows in the join table, with "null","null".
@Juan, for page caching I recommend using a subdomain or something in the routes to separate the mobile version from the full version. See my response to Alex earlier.
Wow, you never disappoint me. I watch your screen casts every monday, and I feel like each week my career is really bolstered by the knowledge I gain from them.
@Brian, yeah, those are two great benefits to building a web app over a native app. And it is fairly easy to port across mobile platforms.
Just be aware of the negatives too. User must be online and app has limited access to hardware (camera, accelerometer, multi-touch, etc.). I have seen ways to do offline caching with jQTouch but haven't tested it.
@Alex, that's a great point. I was considering doing this with a subdomain but it was too much to cover in this episode. I do recommend using a subdomain though. The nice thing is, if you build your site around the "mobile_devices?" method, it is very easy to swap out the behavior depending on subdomain, user settings, etc.
As a mobile device and desktop user, I'm a bit concerned about your approach - you're basically returning different content for the same URL, depending on whether I come from a mobile phone or a desktop, aren't you?
Wouldn't it be better to redirect mobile users to a different URL, and serve consistently cachable content from the same URL?
Mapping URL:content on a 1:1 basis is the RESTful way of doing things. That way I could access the "desktop" version of the site from a if I so wished, or the mobile version from the desktop - all without changing the way the site behaves for me normally.
@Matt, good question. I looked into this briefly but did not have enough time to fully investigate it. The register_alias method has a 3rd argument called extension_synonyms which I was hoping would do this but I could not get it to work. If you do find a solution please let me know!
Great cast Ryan. I noticed that you created a mobile-format version of every view file... is there an easy way to tell Rails to automatically default to the HTML version if a mobile version of the view doesn't exist? I have tons of views in my app, and the truth is that only some sections of the app really need special mobile treatment and I don't have the time to make ".mobile.erb" files for every view. If I don't create a mobile version of a specific view, then I get a "Template is missing" error that says that "index.erb" does not exist (for example).
I'd like to introduce the ActiveDevice (http://github.com/shenoudab/active_device), Our Rails Gem and Plug-in for Device Detection (Mobile, Desktop Browser, Bot). with many helper methods for checking and detect the Mobile Brand, Model, Engine and others ..
Risking repeating what others have said, this is exactly what I was looking for. Can't believe the good timing.
For the shake of completeness: I think today's post is missing the contents of the mobile.js file (even if it's just one line, for initializing jqtouch)
That screencast was totally awesome. When I saw the title I thought that I would knew everything that you could possibly show in this screencast... but apparently I was wrong, I learned many things. Thank you very much again! :)
Ryan, you timing could not have been better. Customizing one my apps for mobile devices is my project of the week and I did not not about JQTouch so that's great.
The rest of the screencast is very helpful too, I will use your mobile_device? method instead of messing with the cumbersome WURFL file and its conversion to MySQL.
Thanks Ryan! I am always a fan of your succinct presentation of useful information. On a side note, I have been playing around with http://www.appcelerator.com/ for making iPhone apps quickly and (dare I say it) easily. Let me know if you've used it. I'd love to hear your thoughts!
great plugin Ryan, during evaluation of it I have created an advanced structure for rights management (have a look at http://niczsoft.com/2010/01/complex-associations-in-rails-activerecord/) and there I have a problem with deep nesting and activerecord, maybe complex associations might be covered by one of next railscasts ?
did anyone figure out how to use paperclip to attach files in ajax forms (form_remote_tag/form_remote_for)? this seems like a big limitation to me.
what are the workarounds?
next I Commenting out the following lines from /vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
like this
#extend ActiveSupport::Memoizable
#memoize :ie_request?
#memoize :ssl_request?
but I get the different error message.
D:/Redmine/redmine-0.8.7/vendor/plugins/prawnto/lib/prawnto/template_handler/com
pile_support.rb:32: undefined method `memoize' for Prawnto::TemplateHandler::Com
pileSupport:Class (NoMethodError)
I spent the day trying to figure out how to use this with single table inheritance. I have the situation where I have a form with many different but quite similar entries. e.g. cars trucks bikes. The information I want to gather is just slightly different for each one.
I've been beating my head against it all day and I think it could d be done by changing the helper code to enstantiate the correct form builder and load the correct partial.
Has anyone done something similar and can give any pointers? It feels like the sort of problem that has a very succinct answer if one only knew what it was :).
hi, i like your cast. but i have a problem with this episode. i get am 406 not acceptable error from the ajax action and i could not find the fault.
i try to set the mime type in the mime_types.rb. my rails version is 2.3.5
First of all, great plugin Ryan! I have a question (probably too simple): I used the :nested option for nested resources, now how do I get the instances of those resources in my Ability class, specifically for the block:
can :action, Model do |model| ...
Having trouble with flash.. with flash[:notice] or the method shown here, any use of flash causes a 'stack level too deep' error. Is there any way to clear the flash ? I assume this would clear the error.
For example, you mentioned in passing that it was a good idea to make sure the functionality wasn't already implemented. Now being about 9 months into RoR I am still learning a lot of things I just didn't know existed.
Sorry
My bad. Ignore the previous one.
I get the following error when I try to use add fields:
undefined method `klass' for nil:NilClass
what seems to be the problem?
Hi! I really enjoy to use prawn + prawnto but have one problem:
My PDF (Rails 2.3.1, Prawn 0.6.3, Prawnto, prawn-core, prawn-format, prawn-layout) is generated from a prawnto-template and gets data from a from which also produces csv, so the parameter format pdf is added in a method which will be called before generating the pdf. The produced PDF has no file extension. How to solve this problem? Tried already respond_to block, but did not help.
Thx, Sandra
THX for this gr8 tutorial Ryan!
Q: How can I set the table headers bold with the inline prawnto?
Good one. As usual ! Keep them coming :o)
This is a little modification for auto detect. If someone find useful:
before_filter :ready_for_mobi
private
MOBILE_BROWSERS = ["android", "ipod", "opera mini", "blackberry", "palm","hiptop","avantgo","plucker", "xiino","blazer","elaine", "windows ce; ppc;", "windows ce; smartphone;","windows ce; iemobile", "up.browser","up.link","mmp","symbian","smartphone", "midp","wap","vodafone","o2","pocket","kindle", "mobile","pda","psp","treo"]
def ready_for_mobi
agent = request.headers["HTTP_USER_AGENT"].downcase
MOBILE_BROWSERS.each do |m|
return request.format = :mobile if agent.match(m)
end
end
so far no issues and errors experienced. much easier than cucumber. thanks for this easy to follow tutorials and instructions.
vinod have a look there http://khamsouk.souvanlasy.com/articles/ajax_file_uploads_in_rails_using_attachment_fu_and_responds_to_parent
I have a question too, is there a way with paperclip to convert png and gif files' alpha channel to a white background?
What if you want a survey to always have at least 1 question field by default?
So when you create a survey, no questions are added.
But when you edit that survey, at least one question field is displayed. When you update the survey, the new question you entered is saved.
I can't currently get this to work. I think update_attributes won't save the new question, but I'm totally lost on how to get it regardless.
Hi Ryan,
Thanks for this episode (as well as all the others).
At the beginning of the episode, you touched on the notion of browsing your development app through a mobile device. I've run into a little problem with this that's probably an obvious fix. When I try to hit my local dev app from any machine other than the computer that is running the dev app, none of the assets, such as stylesheets and javascript, load. When I look at the html, I see that "localhost:3000" is embedded in the urls for those assets. What should I change to make this work properly?
My current dev setup is:
- Mac OS X 10.6
- Rail 2.3.5
- start dev app using "script/server" which uses mongrel
Thanks!
What if you always want to start with at least one question? Let's say a user submits a survey with no questions, but then wants to add a question when editing the survey?
I know this can be done via javascript (see part 2), but how would I do this without javascript?
I'm basically looking to get the edit view to populate with at least one question field even if that question doesn't exist, and when update_attributes is called to add the new question that was entered.
At the moment, I can do:
If @survey.questions.empty?
@survey.questions.build
end
In my edit method, but the update method is not saving the new question added when editing a survey.
Help!
Hi Ryan
I created a blog with similar information recently:
http://www.arctickiwi.com/blog/2-mobile-enable-your-ruby-on-rails-site-for-small-screens
This just uses a different application layout if the device is detected to be mobile.
Cheers
Hey Ryan,
Big fan of your work! I was trying out the code, but ran into a bit of an issue.
The join table is saving "null","null" as the product_id, category_id. When I have 2 categories selected, I have 2 rows in the join table, with "null","null".
I'm using Mysql 5, and exactly the code above.
Same error as 34 & 37
"There's an error with this transaction. Please enter a complete billing address."
@Juan, for page caching I recommend using a subdomain or something in the routes to separate the mobile version from the full version. See my response to Alex earlier.
Wow, you never disappoint me. I watch your screen casts every monday, and I feel like each week my career is really bolstered by the knowledge I gain from them.
Thank you.
Ryan, how do you manage page caching with this technique?
@Brian, yeah, those are two great benefits to building a web app over a native app. And it is fairly easy to port across mobile platforms.
Just be aware of the negatives too. User must be online and app has limited access to hardware (camera, accelerometer, multi-touch, etc.). I have seen ways to do offline caching with jQTouch but haven't tested it.
@Alex, that's a great point. I was considering doing this with a subdomain but it was too much to cover in this episode. I do recommend using a subdomain though. The nice thing is, if you build your site around the "mobile_devices?" method, it is very easy to swap out the behavior depending on subdomain, user settings, etc.
As a mobile device and desktop user, I'm a bit concerned about your approach - you're basically returning different content for the same URL, depending on whether I come from a mobile phone or a desktop, aren't you?
Wouldn't it be better to redirect mobile users to a different URL, and serve consistently cachable content from the same URL?
Mapping URL:content on a 1:1 basis is the RESTful way of doing things. That way I could access the "desktop" version of the site from a if I so wished, or the mobile version from the desktop - all without changing the way the site behaves for me normally.
you can change your useragent in firefox too, using this plugin: https://addons.mozilla.org/en-US/firefox/addon/967
although this is not as convenient as the drop-down list in safari, it still works :)
adding text/javascript to /etc/mime.types solve the problem.
@AJ - Rails is looking for the layout that nifty_scaffold generated. Try "script/generate nifty_layout".
Wow great stuff Ryan!
The web app (instead of native iphone app) looks like a good options for a few reasons:
1. you can keep all your code under one project (easier to manage over time as you add/remove features)
2. Don't have to go through Apple's review process
Does that seem accurate in your experience? Thanks for your great work!
@egarcia, thanks! Added.
@Matt, good question. I looked into this briefly but did not have enough time to fully investigate it. The register_alias method has a 3rd argument called extension_synonyms which I was hoping would do this but I could not get it to work. If you do find a solution please let me know!
Great cast, note that you could also use the Android simulator to check the looks, especially since Android is growing rapidly on the web!
Emulator is available through Google: http://developer.android.com/guide/developing/tools/emulator.html
Great cast Ryan. I noticed that you created a mobile-format version of every view file... is there an easy way to tell Rails to automatically default to the HTML version if a mobile version of the view doesn't exist? I have tons of views in my app, and the truth is that only some sections of the app really need special mobile treatment and I don't have the time to make ".mobile.erb" files for every view. If I don't create a mobile version of a specific view, then I get a "Template is missing" error that says that "index.erb" does not exist (for example).
Hello Ryan,
Can you add url to list
http://github.com/brendanlim/mobile-fu/ ?
Automatically detect mobile requests from mobile devices in your Rails application
Very cool, good work as always - Brendan Lim's mobile fu is a good solution here as well:
http://github.com/brendanlim/mobile-fu
@Adam King - agreed, Appcelerator Titanium rocks! If you're interested in doing a native app with web tech, check it out.
Thanks for the great cast! as usual.
I'd like to introduce the ActiveDevice (http://github.com/shenoudab/active_device), Our Rails Gem and Plug-in for Device Detection (Mobile, Desktop Browser, Bot). with many helper methods for checking and detect the Mobile Brand, Model, Engine and others ..
Risking repeating what others have said, this is exactly what I was looking for. Can't believe the good timing.
For the shake of completeness: I think today's post is missing the contents of the mobile.js file (even if it's just one line, for initializing jqtouch)
Thanks again!
That screencast was totally awesome. When I saw the title I thought that I would knew everything that you could possibly show in this screencast... but apparently I was wrong, I learned many things. Thank you very much again! :)
Ryan, you timing could not have been better. Customizing one my apps for mobile devices is my project of the week and I did not not about JQTouch so that's great.
The rest of the screencast is very helpful too, I will use your mobile_device? method instead of messing with the cumbersome WURFL file and its conversion to MySQL.
Thanks again and have a great week.
Thanks Ryan! I am always a fan of your succinct presentation of useful information. On a side note, I have been playing around with http://www.appcelerator.com/ for making iPhone apps quickly and (dare I say it) easily. Let me know if you've used it. I'd love to hear your thoughts!
Adam
great plugin Ryan, during evaluation of it I have created an advanced structure for rights management (have a look at http://niczsoft.com/2010/01/complex-associations-in-rails-activerecord/) and there I have a problem with deep nesting and activerecord, maybe complex associations might be covered by one of next railscasts ?
did anyone figure out how to use paperclip to attach files in ajax forms (form_remote_tag/form_remote_for)? this seems like a big limitation to me.
what are the workarounds?
great tutorial ryan!
thanks
After installing prawn and prawnto I get this error when I try and run the server.
D:/Redmine/redmine-0.8.7/vendor/rails/activesupport/lib/active_support/dependenc
ies.rb:276:in `load_missing_constant': uninitialized constant ActiveSupport::Mem
oizable (NameError)
next I Commenting out the following lines from /vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
like this
#extend ActiveSupport::Memoizable
#memoize :ie_request?
#memoize :ssl_request?
but I get the different error message.
D:/Redmine/redmine-0.8.7/vendor/plugins/prawnto/lib/prawnto/template_handler/com
pile_support.rb:32: undefined method `memoize' for Prawnto::TemplateHandler::Com
pileSupport:Class (NoMethodError)
Now being about 9 months into RoR I am still learning a lot of things I just didn't know existed.
Great screencast as usual.
I spent the day trying to figure out how to use this with single table inheritance. I have the situation where I have a form with many different but quite similar entries. e.g. cars trucks bikes. The information I want to gather is just slightly different for each one.
I've been beating my head against it all day and I think it could d be done by changing the helper code to enstantiate the correct form builder and load the correct partial.
Has anyone done something similar and can give any pointers? It feels like the sort of problem that has a very succinct answer if one only knew what it was :).
Does this work under the new versions of Rails? I'm having trouble with it.
hi, i like your cast. but i have a problem with this episode. i get am 406 not acceptable error from the ajax action and i could not find the fault.
i try to set the mime type in the mime_types.rb. my rails version is 2.3.5
Would be interesting to combine webforms2 with this. Following example is screaming at this screencast :)
http://webforms2.googlecode.com/svn/trunk/testsuite/003.html
First of all, great plugin Ryan! I have a question (probably too simple): I used the :nested option for nested resources, now how do I get the instances of those resources in my Ability class, specifically for the block:
can :action, Model do |model| ...
Hi Javon
There is a method called "Excerpts" that was supposed to be implemented in the latest version of thinking sphinx
Check that, it may help you
Having trouble with flash.. with flash[:notice] or the method shown here, any use of flash causes a 'stack level too deep' error. Is there any way to clear the flash ? I assume this would clear the error.
Bob
Since this screen cast was made 2 years ago, I suspect a lot of this is out of date.
Now we would use
accepts_nested_attributes_for :tasks
and then do something like
form_for @project do |project_form|
project_form.fields for :tasks ...
Right? Or is that different funcionality?
How would stay this example if I want to use the Authlogic? thank you ...
For example, you mentioned in passing that it was a good idea to make sure the functionality wasn't already implemented. Now being about 9 months into RoR I am still learning a lot of things I just didn't know existed.
also +1 for formtastic
One of my favorite episodes.
Provide software to enter image descriptions and support all picture file formats.