I've been working on this same setup using upgrades for an analysis website, turning comments/upgrades into a polymorphic generic.
In the cast, you use a hack of :id => nil to get back to the page you were on before. This seems to only work if your creation form is on the index page. In my scenario, I kept the comment creation form on the new action. It took me a while, but I found a way to keep using RESTful routing and the polymorphic generic
controller.
The code: http://pastie.org/444106
Since we're storing the type, we use it to form a string version of the RESTful route, then eval() in the redirect_to. I'm not sure if it's any less 'hackish' than the :id => nil, but it's at least a bit more versatile in where you're coming from before the create action.
Just thought I'd throw out how I did it for folks that might stumble into the same situation.
First off you are really helping people adopt rails.
Excellent work, really. thanks !
Couple of questions.
when you do script/console and modified a model,
1) why don't you have to do a model.save to apply the updates to the database. Looks like it does that automatically. why ?
2) i see you do model.build how is it different from model.save or model.new ?
First off you are really help people adopt rails.
Execellent work, really. thanks !
when you do script/console and modified product,
1) why didn't you have to do a product.save to apply the updates to the database. Looks like it did that automatically. why or rather how ?
2) i see you do model.build how is it different from model.save or model.new ?
The problem with the polymorphic association technique is that it doesn't allow you to use foreign key constraints to ensure data integrity. I don't use it for that reason alone.
@Gavin - consider the party supertype/subtype model: companies and customers would both have parties as their supertype. Parties would then have many addresses. It's a very common pattern. Google it for more info.
Always love the screencasts Ryan. I have rails 2.2.2 I installed the gem and plugin fine but when I just add a views/show/calls.15.pdf file which at this point is just a text.pdf "Hello world" and point my browser to it It doesn't even open the file so give me the show page "Hello world". I jump to the Acrobat app - it asks if I want to open the file and then it says it is not supported when I try to open it. I get nothing like the screencast. do I need to update rails? Thanks
Hi, I had to do something today as Duminy and I got the same mistake.
I read somewhere that it may be the fact that what we are passing to Model.new or Model.update is no longer simple hash because it contains an array inside...
I am still confused but I get as well stringify_keys! method missing.
Thanks for the help
Oops. I was using this in combination with the "group_by" railscast and I was trying to group on a field that had two entries that were nil. I changed my find(:all) to filter out nil values and everything works now.
Everything works great when I actually include the search term in the URL (http://localhost:3000/products?search=yard) but not when using the "normal" URL (http://localhost:3000/products). That results in a "NoMethodErorr".
I used restful authentication twice. the first time, it was OK. but the 2nd time it's just not working. I've done exactly as in this screencast but I can access to my controllers without logging as if Restful_authentication is not installed.
Thanks for a help!
Thanks for the screencast! I only have one issue. I'm making a school website where students can type there reports online. I'm using tinymce as editor, but tiny stores the text as HTML in the database.
When I display this text in PDF you see all HTML tags. How can I remove these ???
Ryan, you are a really mean guy, you know? I wanted to use the easter weekend to get forward with my app, but I guess, now I'm gonna play around with cukes all day long. ;)
A lot of plugins require you to edit various files for installation. For example, the rails-authorization-plugin requires you to add some stuff to environment.rb.
restful authentication requires you to add include AuthenticatedSystem to your application_controller.
Like many folks here I used Ryan's suggestion and used the singular form of "session" during generate and I get the same NameError in SessionsController#new
Paul's suggestion (Posting 84) as below
>map.resource :session
>use rather
> map.resource :session, >:controller => 'session'
makes the whole thing go in an infinite loop that FF aborts out of.
Ryan - It will help the user community save hours of figuring this out if you can suggest the right way to fix it when using singular form of session. Thanks.
Hi
I did exactly what is there in the video and its working, But whenever i try to update the same i got the following error
undefined method `stringify_keys!' for "6":String
My Models are
*************
class Product < ActiveRecord::Base
has_and_belongs_to_many :members
belongs_to :category
has_many :installments
def installment_attributes=(installment_attributes)
installment_attributes.each do |attributes|
installments.build(attributes)
end
end
end
*******************************
class Installment < ActiveRecord::Base
belongs_to :product
end
_______________________________
and the products_controller looks like this->
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@product) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
and the stack trace is->
Awesome! But where did you get the textmate bundle? I've been looking all over for it. My textmate doesn't color my .pdf.prawn files and dangit, I'm jealous!
Further to the above - what it appears to be doing is rendering the first page of results.. over and over again, 10 times.
(there are more than 10 pages of results so I'm not sure why this is).
So somehow the page parameter is being misused, or ignored altogether, by this method. Has anyone else had a similar problem?
What am I missing here?
Hi, I got past the above by amending the line to:
page.insert_html :bottom, :debtors, :partial => 'debtors', :collection => @debtors.
However, I don't think the checkScroll method is working at all.. it simply starts rendering all pages immediately - which also means its a very slow page load! I have tried altering the ScrollDistanceFromBottom to various values but it makes no difference.
Also the 'loading more results' never gets hidden.
For context, I am listing search results beneath a search form. I think that when the search form is submitted, the scroll is already near the bottom of the page - could this be the problem?
Any suggestions gratefully received.
Hi, love your screencast, but I am having trouble getting this to work.
I get the following error:
ActionView::TemplateError (undefined method `intern' for #<WillPaginate::Collection:0x262a7f8>) on line #1 of app/views/clientweb/list.rjs:
1: page.insert_html :bottom, :debtors, :partial => @debtors
2: if @debtors.total_pages > @debtors.current_page
3: page.call 'checkScroll'
4: else
Is this anything to do with WillPaginate versions? Any ideas?
I can't get any further with this, and it seems a shame if I have to abandon it.
Hi! Thanks for the cast. But I found one sad thing for me - no support of other languages. I have russian strings in utf8 and prawn writes them as '______'. I think this is the font problem, but I did not found a solution. Any ideas?
@Rupak, webrat should already be required by the config.gem line in the test.rb environment file which is why I didn't need to require it at the top of the test_helper.rb. Did that not work for you?
Hi Ryan as ever your screencast rocks!
I have a question, simple i think but i cant figure out a smart solution.
I have a model Product and i wanna other 2 models (example, Cigarettes and Wine) that share some caracteristics of Product but have also things that are different from Product.
What is the best solution to create this two model? Create a polymorphic association for Product or, STI ?
Thanks.
Hi Ryan,
Great screencast as usual. I just happened to notice that in the portion for test_helper.rb where you add the webrat configure command, you miss to highlight the point that a line for require 'webrat' needs to be added to the top of the file as well. Some users can get confused when they run rake test:integration and get an error as it cannot find Webrat. Hope it helps.
I don't care that I am the 160th person to say some variation of this...you are awesome! Just saved me hours, and got some new rails code architecture tips
Great screencast, Ryan! Thanks for covering Webrat.
For anyone interested in Webrat's Selenium support... grab the 0.4.4 gem I just released (once it's made it to the gem mirrors, or directly from git).
Fixed a couple bugs and added much better output to explain what's going on in that mode. I'll do a write up on my blog on how to run the example from this screencast in Selenium ASAP.
@John, I've had the same problem as you, i.e. trying to make paperclip work with RJS & AJAX, and getting the "photo must be set" error.. Apparently, It's got something to do with AJAX not being able to read the local file system... I will continue to investigate, and would really appreciate if anyone else with a solution could post it here...
Thanks for this Railscast. I am trying to parse an RSS feed to add data to a new Rails site and this has been a big help.
I've been working on this same setup using upgrades for an analysis website, turning comments/upgrades into a polymorphic generic.
In the cast, you use a hack of :id => nil to get back to the page you were on before. This seems to only work if your creation form is on the index page. In my scenario, I kept the comment creation form on the new action. It took me a while, but I found a way to keep using RESTful routing and the polymorphic generic
controller.
The code: http://pastie.org/444106
Since we're storing the type, we use it to form a string version of the RESTful route, then eval() in the redirect_to. I'm not sure if it's any less 'hackish' than the :id => nil, but it's at least a bit more versatile in where you're coming from before the create action.
Just thought I'd throw out how I did it for folks that might stumble into the same situation.
very useful code, thanks
Every time i try to install the plugin i get the following line:
removing: C:/rails/rails_apps/QTI/vendor/plugins/prawnto/.git
I am using this:
ruby script/plugin install git://github.com/thorny-sun/prawnto.git
any hints?
@schreyans, googling for that same question got me your question ;), so googled some more, thought you might want me to share it.
search in http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html for build_, much documentation on it! hope this helps :)
If anyone is having trouble uninstalling a gem from your home directory ~/.gem please check out this link:
http://blog.floehopper.org/articles/2009/02/16/uninstalling-gems-from-gem-directory
This was frustrating for me and I had to do this to fix it:
gem uninstall <gem-name> --install-dir=~/.gem/ruby/1.8/
and then run "sudo rake gems:install"
First off you are really helping people adopt rails.
Excellent work, really. thanks !
Couple of questions.
when you do script/console and modified a model,
1) why don't you have to do a model.save to apply the updates to the database. Looks like it does that automatically. why ?
2) i see you do model.build how is it different from model.save or model.new ?
thanks again.
First off you are really help people adopt rails.
Execellent work, really. thanks !
when you do script/console and modified product,
1) why didn't you have to do a product.save to apply the updates to the database. Looks like it did that automatically. why or rather how ?
2) i see you do model.build how is it different from model.save or model.new ?
Excellent railcast, Ryan! Thank you very much. It answered all questions that I had about Rails 2.3 I18n features :)
The problem with the polymorphic association technique is that it doesn't allow you to use foreign key constraints to ensure data integrity. I don't use it for that reason alone.
@Gavin - consider the party supertype/subtype model: companies and customers would both have parties as their supertype. Parties would then have many addresses. It's a very common pattern. Google it for more info.
Always love the screencasts Ryan. I have rails 2.2.2 I installed the gem and plugin fine but when I just add a views/show/calls.15.pdf file which at this point is just a text.pdf "Hello world" and point my browser to it It doesn't even open the file so give me the show page "Hello world". I jump to the Acrobat app - it asks if I want to open the file and then it says it is not supported when I try to open it. I get nothing like the screencast. do I need to update rails? Thanks
i am very much new to rails.This gave me a very good idea.Thank u. please post good tutorials from scratch so that it gives a fair idea for us.
Hi, I had to do something today as Duminy and I got the same mistake.
I read somewhere that it may be the fact that what we are passing to Model.new or Model.update is no longer simple hash because it contains an array inside...
I am still confused but I get as well stringify_keys! method missing.
Thanks for the help
Oops. I was using this in combination with the "group_by" railscast and I was trying to group on a field that had two entries that were nil. I changed my find(:all) to filter out nil values and everything works now.
Everything works great when I actually include the search term in the URL (http://localhost:3000/products?search=yard) but not when using the "normal" URL (http://localhost:3000/products). That results in a "NoMethodErorr".
How can I get the default form to work?
I used restful authentication twice. the first time, it was OK. but the 2nd time it's just not working. I've done exactly as in this screencast but I can access to my controllers without logging as if Restful_authentication is not installed.
Thanks for a help!
Jeremy: git is a version control system. You will have to install it before you can clone from github. It doesn't take long.
I'd still like to know where I can find the textmate bundle for coloring my prawn files...
Thanks for the screencast! I only have one issue. I'm making a school website where students can type there reports online. I'm using tinymce as editor, but tiny stores the text as HTML in the database.
When I display this text in PDF you see all HTML tags. How can I remove these ???
Thanks..
Thanks again for showing webrat! We just started with Cucumber, but this is easier!
Thanks Ryan,
A globalize2 screencast would be Great!
Thanks for the cast. Excited about using Prawnto but I'm having trouble installing the plugin. When ever I run the command it says:
removing: .../vendor/plugins/prawnto/.git
sh: git: command not found
Any help for the noob?
Thanks
Ryan, you are a really mean guy, you know? I wanted to use the easter weekend to get forward with my app, but I guess, now I'm gonna play around with cukes all day long. ;)
w.r.t postings 99 and 100. The error was a beginner mistake - had the before filter setup that was taking me in a loop.
Ryan - These sessions are very helpful. Appreciate your doing it.
Still not very clear on what map.resource :session, :controller => 'session' accomplishes but will figure that out. Thanks.
Thanks for the screencast!
How can I do this in smarter way?
(OR) conditions??
Something.find(:all, :conditions => ["user_id=? OR is_public=?", current_user.id, true])
Basically, I want to search for something that is belong to user, or public..
@Jacob, interesting. Are you using the latest version of Rails? Do you have any other testing plugins installed which may be interfering with this?
Awesome!
Thanks for the railscast! as usual superb!
A lot of plugins require you to edit various files for installation. For example, the rails-authorization-plugin requires you to add some stuff to environment.rb.
restful authentication requires you to add include AuthenticatedSystem to your application_controller.
Is there a good way to handle this?
For completeness, I tried Ryan's suggestion in comment 34 and changed
map.resource :session
to
map.resource :session, :controller => 'session'
in routes.rb and that caused FF to go in an infinite loop as well. I am on 2.2.2 with FF 3.0.8
Anyone else seeing this problem?
Like many folks here I used Ryan's suggestion and used the singular form of "session" during generate and I get the same NameError in SessionsController#new
Paul's suggestion (Posting 84) as below
>map.resource :session
>use rather
> map.resource :session, >:controller => 'session'
makes the whole thing go in an infinite loop that FF aborts out of.
Ryan - It will help the user community save hours of figuring this out if you can suggest the right way to fix it when using singular form of session. Thanks.
I've been trying to get this to work but every time I try to run the test I get `test': wrong number of arguments (1 for 2) (ArgumentError).
I copied the tests from the episode so that I would have a starting point so there are no typos. I'm lost on this one.
Hi
I did exactly what is there in the video and its working, But whenever i try to update the same i got the following error
undefined method `stringify_keys!' for "6":String
My Models are
*************
class Product < ActiveRecord::Base
has_and_belongs_to_many :members
belongs_to :category
has_many :installments
def installment_attributes=(installment_attributes)
installment_attributes.each do |attributes|
installments.build(attributes)
end
end
end
*******************************
class Installment < ActiveRecord::Base
belongs_to :product
end
_______________________________
and the products_controller looks like this->
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@product) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
and the stack trace is->
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2736:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2439:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/reflection.rb:162:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/reflection.rb:162:in `build_association'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:418:in `build_record'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:102:in `build'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:100:in `build'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:100:in `collect'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:100:in `build'
/home/dwija/matrix/app/models/product.rb:8:in `installment_attributes='
/home/dwija/matrix/app/models/product.rb:7:in `each'
/home/dwija/matrix/app/models/product.rb:7:in `installment_attributes='
/home/dwija/matrix/app/controllers/products_controller.rb:65:in `update'
/home/dwija/matrix/app/controllers/products_controller.rb:64:in `update'
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
I could not understand why the error is coming ?
Any advice?
Duminy
Awesome! But where did you get the textmate bundle? I've been looking all over for it. My textmate doesn't color my .pdf.prawn files and dangit, I'm jealous!
I just installed all this and the default webrat file was missing the go to method (amongst others). Also didn't have the routes file, either.
I lifted them from your git repository and then webrat started complaining that it wasn't configured properly and needed
Webrat.configure do |config|
config.mode = :rails
end
Suspect the gems have moved on but as usual the documentation doesn't tell you.
Further to the above - what it appears to be doing is rendering the first page of results.. over and over again, 10 times.
(there are more than 10 pages of results so I'm not sure why this is).
So somehow the page parameter is being misused, or ignored altogether, by this method. Has anyone else had a similar problem?
What am I missing here?
@Kenneth:
Try to use this tutorial to install Cygwin as a Unix-like terminal:
Cygwin Info: http://en.wikipedia.org/wiki/Cygwin
Tutorial for rails: http://phaseshiftllc.com/archives/2008/10/02/setting-up-rails-on-windows-with-cygwin
- Florian
Hi, I got past the above by amending the line to:
page.insert_html :bottom, :debtors, :partial => 'debtors', :collection => @debtors.
However, I don't think the checkScroll method is working at all.. it simply starts rendering all pages immediately - which also means its a very slow page load! I have tried altering the ScrollDistanceFromBottom to various values but it makes no difference.
Also the 'loading more results' never gets hidden.
For context, I am listing search results beneath a search form. I think that when the search form is submitted, the scroll is already near the bottom of the page - could this be the problem?
Any suggestions gratefully received.
Hi, love your screencast, but I am having trouble getting this to work.
I get the following error:
ActionView::TemplateError (undefined method `intern' for #<WillPaginate::Collection:0x262a7f8>) on line #1 of app/views/clientweb/list.rjs:
1: page.insert_html :bottom, :debtors, :partial => @debtors
2: if @debtors.total_pages > @debtors.current_page
3: page.call 'checkScroll'
4: else
Is this anything to do with WillPaginate versions? Any ideas?
I can't get any further with this, and it seems a shame if I have to abandon it.
Best regards.
I have setup Cucumber as outlined in the Railscast. Cucumber on a Windows Vista System console does not show correct (ansi escape coded) colors.
How to setup Windows Vista Home Premium w Rails 2.3, cucumber, rspec,
rspec-rails, webrat for proper Cucumber console colors.
Superb intro to Cucumber. Many thanks.
Ken Wagner
Hi! Thanks for the cast. But I found one sad thing for me - no support of other languages. I have russian strings in utf8 and prawn writes them as '______'. I think this is the font problem, but I did not found a solution. Any ideas?
@Rupak, webrat should already be required by the config.gem line in the test.rb environment file which is why I didn't need to require it at the top of the test_helper.rb. Did that not work for you?
For those on Linux using FF 3 (Selenium mode) -
config.selenium_browser_key =
'*firefox3 /opt/firefox/firefox-bin'
binary path may vary, space is required.
keep it up.
Hi Ryan as ever your screencast rocks!
I have a question, simple i think but i cant figure out a smart solution.
I have a model Product and i wanna other 2 models (example, Cigarettes and Wine) that share some caracteristics of Product but have also things that are different from Product.
What is the best solution to create this two model? Create a polymorphic association for Product or, STI ?
Thanks.
Hi Ryan,
Great screencast as usual. I just happened to notice that in the portion for test_helper.rb where you add the webrat configure command, you miss to highlight the point that a line for require 'webrat' needs to be added to the top of the file as well. Some users can get confused when they run rake test:integration and get an error as it cannot find Webrat. Hope it helps.
Thanks,
Rupak Ganguly
Thanks for providing those instructions Bryan! I'll link to them in the show notes for the episode.
Hey, great episode! I definitely have to try BDD with cucumber.
I don't care that I am the 160th person to say some variation of this...you are awesome! Just saved me hours, and got some new rails code architecture tips
As promised, here are instructions for running those exact same tests with Webrat's Selenium mode:
http://www.brynary.com/2009/4/6/switching-webrat-to-selenium-mode
Great screencast, Ryan! Thanks for covering Webrat.
For anyone interested in Webrat's Selenium support... grab the 0.4.4 gem I just released (once it's made it to the gem mirrors, or directly from git).
Fixed a couple bugs and added much better output to explain what's going on in that mode. I'll do a write up on my blog on how to run the example from this screencast in Selenium ASAP.
Cheers,
-Bryan
@John, I've had the same problem as you, i.e. trying to make paperclip work with RJS & AJAX, and getting the "photo must be set" error.. Apparently, It's got something to do with AJAX not being able to read the local file system... I will continue to investigate, and would really appreciate if anyone else with a solution could post it here...
Cheers