The code you've posted here isn't the same as that in the Advanced Rails Recipes book (e.g. the task partial is significantly different). I'm wondering which code is more up-to-date, particularly in the light of errata item 31586 posted at the book's site.
I've blogged about how to get around the issue of the radio buttons having the same ids at "http://zilkey.com/2008/4/5/complex-forms-with-correct-ids":http://zilkey.com/2008/4/5/complex-forms-with-correct-ids
In general I try to mirror all simple rails validations (presence, length, uniqueness etc...) with indexes and not-null fields in the database. In your screencast you stub out valid? to bypass validations, but if there were any not-null fields in your table that spec would fail.
If you added a not-null constraint to your database at a later date, you'd have to go to that spec and update the post methods to include params that correspond with the not-null fields, which can quickly become unmaintainable since your controller specs are now tied to your model's fields.
I think in your example stubbing save would have been the better way to go. I typically mock the entire class, but it's a lot of work when you use integrate_views. Stubbing save would also make the should_be new_record call unnecessary.
Thanks for the great screencasts - I've learned a lot from you.f
Ryan, I am fairly a newcomer to the Ruby on Rails world over from Coldfusion. I must admit that your 'FREE' Railscasts are simply superb! Keep up the good work, you have my support!
I am having a problem displaying my data in the view side, I am using partials.
<%= will_paginate @my_object %>
and I only see the pagination links on the browser but I can't see the data, although when I try to loop through @my_object without using will_paginate I was able to get the data, I don't know what I am doing wrong I followed the same steps from this screencast.
This will create a migration that adds foo, bar and baz to the table "tablename" as expected, even though those fields are not identified in the migration name.
Ryan, you just saved my day with Railscast #94 and #95! For HOURS I was looking for a short and neat little howto in ActiveResource as the one you made. Muchas gracias! :-)
@Neil - To answer the PS, the reason it does that is the route builder defaults to :index if you do not pass an action. When you want to ask if something is the current page it compares the route to the hash passed. If you don't pass an action, it assumes you mean index.
If you want to check the current controller only you could use something like
def current_controller?(*attrs)
attrs.collect{|a| a.to_s}.include?(@controller.controller_name)
end
Then, in your view you can find out if you are in a particular controller (or even array of controllers) like...
@DAZ - both of those helpers are built in to Rails. Check them out on the API.
Great episode, Ryan. I'll be coming back to this one a few more times. I'd agree with the other guys about the display:none, though.
I started using the current_page? method for css hooks on navigation and subsequently ran in to the appended url issue during search/pagination. I've yet to find a clean solution for a two tier tabbed navigation with highlighted current tabs - maybe this would make a good follow-up episode? I'd be interested in seeing the workarounds for the url comparison...
p.s. I find it strange that current_page? (:controller => :users) only returns true on the index action. Has anyone else noticed this?
Love the RailsCasts. I also look for them every Monday morning.
I also have to agree that hiding things via CSS that really shouldn't be displayed just strikes me as a bad idea. There may be situations where it might be OK, but I really can't think of any offhand.
However, I did love the 2nd example. How is it I've made it this far without realizing I could pass blocks to a yield in a partial? THAT is going to be extremely helpful, I think.
I'd have to agree with Nicolash, hiding content that should never be displayed through css seems like a bad idea. Also having partials specify what stylesheets to load sounds like asking for problems maintaining the app later on but maybe that's just me ;)
Other than that another great 'cast! Hard to believe it'll be 100 next week already.
the third example (style-sheet trick) has some negative implications for the accessibility as screenreaders still don't treat display:none consitantly.
Some will still read the link to the user, this may not be as bad in a single occasion but makes this technic to an anti-pattern in my view as this will accumulate to many wrong placed/unnecessary content for some of the users.
The first thing i do on monday morning is check the new railscasts episode. Thank you for your little but powerful tips and hints :D
Greetings from Germany
nice screencast!!! I have a small question about the cache_sweeper. What i do, if i have more controllers, or conjobs to modify the product model. What is the best way to handle this fact?
@David Blankenship, good question. I would probably hide the link when javascript is disabled, then you could handle adding tasks through a non multi-model form by going through a tasks controller "new" action for each task after the project has been created.
Thank you everyone for your excellent comments. Sorry I haven't gotten around to respond to them all.
I just updated the code in the notes to use a better approach. This is taken from the recipe I wrote for Advanced Rails Recipes. Hopefully it will solve the majority of these problems.
@Carl, I did some previous episodes on optimization, but they don't cover everything so you can expect some more in the future. You can find them out here.
http://railscasts.com/tags/1
Thanks for the great podcast. I am still very new at programming in Rails, so please forgive this comment if it's already obvious to everyone else.
I was using Prototype Javascript 1.5.0 as part of RailsI so I too was getting the "document.observe is not a function" error message. I decided to upgrade to "RailsII" which includes Prototype 1.6.0.
But the error persisted, until I noticed that the "public/javascripts" folder in my application code tree that I had copied over from RailsI included the older versions of the Prototype library.
Just copying the newer Prototype library into the "public/javascripts" folder seemed to do the trick.
Hope this is helpful.
I was wondering if anyone else was having issues with the nested fields for? if i use a boolean in the nested fields it creates multiple records (and thus causes validation issues)
Ryan, how would you go about adapting this to select boxes rather than checkboxes? I have a habtm relationship between "stores" and "products", where each store can carry up to 10 'products', but I would like to display the 'products' in select rather than as checkboxes (as there are > 100 of them).
So, visualize looking at a 'store' page where you see 10 select boxes, all populated from the 'products' table.
I've been trying collection select and friends, but can't seem to get everything to play nicely together.
Thank you! I have been looking for this type of tutorial for, like, two weeks now! Bless you my son!
one quick question, though. Is there a way to combine this with pagination? I think I kind of know how that would work with scoping and what not, but I'm not sure.
question about plugins:
I spent alot of time trying to install the restful_authentication plugin, but when I'm at work behind to corporate firewall it doesn't find the install repository. But when I did it on my home DSL connection, it installed first time.
At work I can go to the techno-weenie site in my browser, so is the problem caused by script/plugin install using a protocol other than http?
Although not the most elegant of solutions, but you can just patch the add method of the BufferedLogger in case you want to keep it.
Create a file custom_logger.rb in your initializers folder and paste the content of the pastie in it (it only puts pid in the log message in production mode, it's less ugly):
http://pastie.caboo.se/171954
Is it possible to get one piece of related data with out actually making a model for it? For example, I have Places and Countries, but I don't need/want a Country model, so I did Place.find(:all, :joins => 'LEFT JOIN countries ON places.country_id = countries.id'). The query executes, but I can't figure out how to access the resulting country name. Does anyone know?
Congratulations!!!
great stuff...
now how would I format this route:
/users/156/items/6547
like this:
/john/items/6547
kriom, there shouldn't be any difference, since content_for_mycontent is only old style version for yield in Rails, as far as I am concerned.
@Eilert Islaksen, I don't think that it's possible otherwise someone would have said something...
@Oskar Lissheim-Boethius, I use Safari and always get the HTTP box to input my details
The code you've posted here isn't the same as that in the Advanced Rails Recipes book (e.g. the task partial is significantly different). I'm wondering which code is more up-to-date, particularly in the light of errata item 31586 posted at the book's site.
Many thanks for your very fine Railscasts.
I've blogged about how to get around the issue of the radio buttons having the same ids at "http://zilkey.com/2008/4/5/complex-forms-with-correct-ids":http://zilkey.com/2008/4/5/complex-forms-with-correct-ids
Yes, I agree. Multiple assertion is a bad practice since only the first assertion gets called if code changes alter behavior.
In general I try to mirror all simple rails validations (presence, length, uniqueness etc...) with indexes and not-null fields in the database. In your screencast you stub out valid? to bypass validations, but if there were any not-null fields in your table that spec would fail.
If you added a not-null constraint to your database at a later date, you'd have to go to that spec and update the post methods to include params that correspond with the not-null fields, which can quickly become unmaintainable since your controller specs are now tied to your model's fields.
I think in your example stubbing save would have been the better way to go. I typically mock the entire class, but it's a lot of work when you use integrate_views. Stubbing save would also make the should_be new_record call unnecessary.
Thanks for the great screencasts - I've learned a lot from you.f
What a great screencast! Thanks for taking the time to put it together.
Hey Ryan - love the railscasts - thank you so much- your pod-cast is like a reference guide to me!
+1 votes for drag and drop cast!
I am caching a lot of actions and some with ids. I was caching them under different folders. My get path uses this string to return:
"/#{params[:controller]/params[:action]"
There is another version with the id tacked on. But expire_action is not removing any of the caches. My call in foo_sweeper looks like this.
expire_action "/foo"
any ideas?
Thanks
Ryan, I am fairly a newcomer to the Ruby on Rails world over from Coldfusion. I must admit that your 'FREE' Railscasts are simply superb! Keep up the good work, you have my support!
Ryan Bates, Hi. :)
Man, I have one problem. :\
look: http://pastie.caboo.se/174710
How are your code by_date?
[]s
tks.
I am having a problem displaying my data in the view side, I am using partials.
<%= will_paginate @my_object %>
and I only see the pagination links on the browser but I can't see the data, although when I try to loop through @my_object without using will_paginate I was able to get the data, I don't know what I am doing wrong I followed the same steps from this screencast.
My fault, the code has to be at the very end of environment.rb -- sorry
It seems to me, that in rails 2.0.2 the code doesn't work. Restarting my mongrel server I get several errors ...
@tommy can you elaborate or Ryan do you have any comments about this one?
Thanks a million for your railscasts. Worth far more than their weight in bytes!
I wanted to point out a subtle feature in these migration generate tasks that may be useful to folks:
As Ryan noted, the migration name is important, primarily in determining if we are adding/removing and to which table.
However, the column that you are adding is less significant. Take the following example:
script/generate migration add_stuff_to_tablename foo:text bar:text baz:text
This will create a migration that adds foo, bar and baz to the table "tablename" as expected, even though those fields are not identified in the migration name.
cheers,
b
Ryan, you just saved my day with Railscast #94 and #95! For HOURS I was looking for a short and neat little howto in ActiveResource as the one you made. Muchas gracias! :-)
@Neil - To answer the PS, the reason it does that is the route builder defaults to :index if you do not pass an action. When you want to ask if something is the current page it compares the route to the hash passed. If you don't pass an action, it assumes you mean index.
If you want to check the current controller only you could use something like
def current_controller?(*attrs)
attrs.collect{|a| a.to_s}.include?(@controller.controller_name)
end
Then, in your view you can find out if you are in a particular controller (or even array of controllers) like...
link_to_unless current_controller?('home'), 'Home', home_path
link_to_unless current_controller?('admin_users', 'admin_posts'), 'Admin', admin_path
@DAZ - both of those helpers are built in to Rails. Check them out on the API.
Great episode, Ryan. I'll be coming back to this one a few more times. I'd agree with the other guys about the display:none, though.
I started using the current_page? method for css hooks on navigation and subsequently ran in to the appended url issue during search/pagination. I've yet to find a clean solution for a two tier tabbed navigation with highlighted current tabs - maybe this would make a good follow-up episode? I'd be interested in seeing the workarounds for the url comparison...
p.s. I find it strange that current_page? (:controller => :users) only returns true on the index action. Has anyone else noticed this?
Great screencast - thanks Ryan. I loved method 2 - something that I'll look at using a bit more.
You seem to have 2 helper methods in your view code:
current_page?
and
simple_format
...are these helpers that you have written yourself or are they built into Rails?
cheers,
DAZ
Is it possible to make groups in a partials (maybe with header and footer)
Thanks for the great work you accomplish.
Love the RailsCasts. I also look for them every Monday morning.
I also have to agree that hiding things via CSS that really shouldn't be displayed just strikes me as a bad idea. There may be situations where it might be OK, but I really can't think of any offhand.
However, I did love the 2nd example. How is it I've made it this far without realizing I could pass blocks to a yield in a partial? THAT is going to be extremely helpful, I think.
Thanks again for the great screencasts.
When using a namespace, is there a path to load the partial. For instance, is there anything like:
form_for([:admin, @post]) do
render :partial => [:admin, @post]
end
to tell the partial to look in /admin/post/_post.html.erb instead of /post/_post.html.erb?
Good stuff as always, but ERB is so 2007... how about some HAML!? ;-) Jack will back me up on this.
I'd have to agree with Nicolash, hiding content that should never be displayed through css seems like a bad idea. Also having partials specify what stylesheets to load sounds like asking for problems maintaining the app later on but maybe that's just me ;)
Other than that another great 'cast! Hard to believe it'll be 100 next week already.
Hi,
Really great tutorials, as always.
How do I apply styling to the feeds.
Thanks in advance
the third example (style-sheet trick) has some negative implications for the accessibility as screenreaders still don't treat display:none consitantly.
Some will still read the link to the user, this may not be as bad in a single occasion but makes this technic to an anti-pattern in my view as this will accumulate to many wrong placed/unnecessary content for some of the users.
i want code for particlur limit of values maintain in textfield
The first thing i do on monday morning is check the new railscasts episode. Thank you for your little but powerful tips and hints :D
Greetings from Germany
@Coop, no idea how to but I'm wondering the same thing.
Hello,
nice screencast!!! I have a small question about the cache_sweeper. What i do, if i have more controllers, or conjobs to modify the product model. What is the best way to handle this fact?
Michael
Thanks for your Railscasts and the latest one's on Rails 2.0! Greets, Danny
@Taylor, thanks for the link. I'll look into doing it. :)
@David Blankenship, good question. I would probably hide the link when javascript is disabled, then you could handle adding tasks through a non multi-model form by going through a tasks controller "new" action for each task after the project has been created.
Thank you everyone for your excellent comments. Sorry I haven't gotten around to respond to them all.
I just updated the code in the notes to use a better approach. This is taken from the recipe I wrote for Advanced Rails Recipes. Hopefully it will solve the majority of these problems.
Hey Ryan, Passenger (mod_rails) is looking for someone to redo there screencast before they release. I think you would be the perfect person for it.
check it out here...
http://izumi.plan99.net/blog/
@Carl, I did some previous episodes on optimization, but they don't cover everything so you can expect some more in the future. You can find them out here.
http://railscasts.com/tags/1
@Sam, thanks. Fixed.
Thanks for the great podcast. I am still very new at programming in Rails, so please forgive this comment if it's already obvious to everyone else.
I was using Prototype Javascript 1.5.0 as part of RailsI so I too was getting the "document.observe is not a function" error message. I decided to upgrade to "RailsII" which includes Prototype 1.6.0.
But the error persisted, until I noticed that the "public/javascripts" folder in my application code tree that I had copied over from RailsI included the older versions of the Prototype library.
Just copying the newer Prototype library into the "public/javascripts" folder seemed to do the trick.
Hope this is helpful.
I was wondering if anyone else was having issues with the nested fields for? if i use a boolean in the nested fields it creates multiple records (and thus causes validation issues)
Any IDE supports GIT to check-in check-out directly like RADRAILS supports SVN
Ryan, how would you go about adapting this to select boxes rather than checkboxes? I have a habtm relationship between "stores" and "products", where each store can carry up to 10 'products', but I would like to display the 'products' in select rather than as checkboxes (as there are > 100 of them).
So, visualize looking at a 'store' page where you see 10 select boxes, all populated from the 'products' table.
I've been trying collection select and friends, but can't seem to get everything to play nicely together.
Thank you! I have been looking for this type of tutorial for, like, two weeks now! Bless you my son!
one quick question, though. Is there a way to combine this with pagination? I think I kind of know how that would work with scoping and what not, but I'm not sure.
question about plugins:
I spent alot of time trying to install the restful_authentication plugin, but when I'm at work behind to corporate firewall it doesn't find the install repository. But when I did it on my home DSL connection, it installed first time.
At work I can go to the techno-weenie site in my browser, so is the problem caused by script/plugin install using a protocol other than http?
Although not the most elegant of solutions, but you can just patch the add method of the BufferedLogger in case you want to keep it.
Create a file custom_logger.rb in your initializers folder and paste the content of the pastie in it (it only puts pid in the log message in production mode, it's less ugly):
http://pastie.caboo.se/171954
Used to work with CVS, got really excited with SVN as replacement, but as linus said, they got it all wrong from the begining....
it's not a technical screencast, but I recomand that video http://www.youtube.com/watch?v=4XpnKHJAok8
a bit long, but very interresting, fun presentation at google talks
Excellent tutorial :-)
Is it possible to get one piece of related data with out actually making a model for it? For example, I have Places and Countries, but I don't need/want a Country model, so I did Place.find(:all, :joins => 'LEFT JOIN countries ON places.country_id = countries.id'). The query executes, but I can't figure out how to access the resulting country name. Does anyone know?
Thanks!
@Sam:
its ruby-perf only. (perf => performance)
Nice screencast. I really need to measure performance issues of my app and tutorials like this are of great help. :)
Little typo detected
should be:
sudo gem install ruby-prof
Great podcast, thanks for the info