Mourad Hammiche: instead of use formatted_categories_path(:js) you can use categories_path and at the :with clause you specify something like "partial=true" or "autocomplete=true", then you handle it in your controller to render a special HTML template for autocomplete. Imao it's better then use the js formatted being not semantic.
Your screencast make programming rails even more easy !
But there is something strange in the convention used for returning the matching categories.
"categories/index.js.erb" assume you returning javascript code ?
How does it works if I need to get real js elsewhere in my project ?
I cannot get autotest to work on XP. I get error message "couldn't find HOME environment.." What should I add to the environment variable setting in Window?
My last comment isn't meant to say that this should've been included in the tutorial; I realize it's definitely in the realm of a separate discussion/tutorial. It's just a suggestion for those actually wanting to implement OpenID.
Everyone should be aware that there is a level of trust that needs to be established before allowing any user to login via OpenID, just as with normal authentication. Trust could be established with a CAPTCHA, whitelist, email verification or other methods.
I think with this there should be a step right after initial OpenID auth that is a CAPTCHA before the account is created to reduce the possibility of spam. This is critical for anyone adding OpenID authentication to his/her system.
OpenID is not inherently trustworthy.
So:
1. User goes to login page
2. User logs in with OpenID
3a. If it's his first time logging in redirect to a CAPTCHA and then, if successful on CAPTCHA, create the account and log the user in.
3b. If he is already on the system authenticate without the CAPTCHA and log the user in.
Hey, love the spiels you cast. Can't get enough of them.
For those with layout issues. If you've defined a layout in application.rb for use site-wide, like application.html.haml or simple.html.erb, then just make another like application.rss.builder or simple.rss.builder (following your convention) using the following code:
xml.instruct! :xml, :version => '1.0'
xml.rss(:version => '2.0') do
xml << yield
end
Put the rest of the RSS code in your index.rss.builder view. Now you can remove the render call and keep your project DRY.
The only downside is builder doesn't indent the yield section of XML -- but that does save you two bytes per item line on output :)
Is it me, or is there a lot of stuff missing if you don't follow these tutorials in order?
I tried following along with this one (Dynamic Select Menus) and all of a sudden kept running into things that were not mentioned in the video: for example, creating the "new" action, creating the Countries and States tables in the db, modifying the routes.rb file (OK, that one was mentioned, just late in the video), etc.
It's impossible to follow along when you don't have the groundwork covered, and now I don't know where the info is that I missed.
@Artūras, a new object is created for each rating, yes. However, I don't think this will be a problem as each object is displayed on the screen. From my understanding, the problem with the old paginator is that each page was an object, so if you have thousands of pages you end up with thousands of objects even though not every one needs to be displayed. You will likely not need to display thousands of ratings at once.
@Jorge, good point! I considered placing this in the lib directory but decided not to because of the similarity these objects have with helpers. Either way I think it's best to make a renderer directory (which I really should have) and add it there.
@bryce, the renderer isn't a controller, so I'm not sure how inheriting from ApplicationController will benefit you. It does have some similarities in how it can render views, but the controller does so much more that this should not be concerned about. Also it's best to keep view code out of the controllers, but I feel okay to put view code in a renderer.
@Ale, I recommend moving this into a plugin if you want to share it between apps. However I find it's easy to get carried away with this and make the code more abstract than it really needs to be. Keep it specific to your application until you really see duplication with another app and then refactor that into a plugin.
@Joran, for me components often have too much overhead. I'm also not sure what benefit it has over this technique.
@James, I generally try to stay away from method_missing, especially in more complex objects when dealing with inheritance. However, the renderer seems simple enough where the downsides of method_missing aren't as apparent. In the code example I gave it's not really necessary, but if you're calling helper methods frequently in a renderer it can be very convenient.
I found it useful for me, so I've made a summary of your screencast and put it in my blog. The permalink is: http://xucros.com/2008/4/18/restful-authenticationin-rails-quickly
Please, let me know if I should remove or change it.
It took me awhile to figure out what projects_path is referring to. It's a variable that is created by adding the line "map.resources :projects" to the routes.rb file. Note that :projects refers to the model name, and that it flows through to the variable. So, if the model is called "visitors", the routes.rb file would you the symbol :visitors and the variable name would be visitors_path.
I hope this helps.
Thanks for the great screen cast. I found it really helpful.
I like this method a lot - I just made one small change:
content_for(:title) { h(page_title) }
Adding the "h" method right to the helper ensures that no matter where I display it, it's safe. I often yield that title in a few other places, like in an h2 element somewhere on the page itself.
Hi,
Just a quick question, do we get a confirmation reply wen we submit our 5 tips to contest@railscasts.com? Just to make sure you really got the mail :) My mail sometimes get stuck in the spam folder :(
Thanks for the great casts Ryan.
For anyone struggling to get this approach to work for date_select or datetime_select etc as mentioned above by @joel see http://dev.rubyonrails.org/ticket/10551
Guys, I'd like to suggest going here www.rubyheroes.com and nominating someone (hint, hint) for his outstanding tutorials, delivered weekly, about Ruby on Rails. Six people will be selected to win an award on stage next month at RailsConf (which, while in my home town, I won't be able to attend this year, boo hoo!) If anyone deserves an award, it would be Ryan, don't you think?
I have a question about the method_missing part. Is it best practice to design your code around exception being thrown? The code in the method_missing part isn't unknown at the time the class is written (like the find_by methods). It seems an expensive way to get content_tag instead of @template.content_tag.
Thanks for the episodes they have been really helpful
Is this not something for which you would want to use a component. I know using rails components is considered bad. But I think this problem could very well be solved with the concept of components. It would be great if rails would get a better and fast implementation for components.
Hello Ryan, thanks for this great episode. I was wondering why this StarRenderer class cannot live in lib/ directory. I use to put all my custom code in there.
Haven't rails default pagination helpers used this model?
Considering you (for example) need to render a rating for like... 100 products it would take 100 objects with their initializations, isn't that a bit too slow?
Another excellent episode (I see you've moved the sponsor ads to the beginning of the shows now too :( ).
I found Railscasts yesterday, and am catching up on all 101 episodes ASAP ^_^ Keep up the great work.
Have you considered a series of screencasts where you make an actual application from scratch, rather than edit an existing one. I know that for some people like myself, it would help get a grasp of which foot to start off on ;)
I know a guestbook was made in 10 minutes (there was a demo somewhere). How about building a forum ? :P I don't think there is one out there atm ^_^
You might not always want to use the -a option with commits.
I like the power of the index just having the state of the files when they were added. Using the -a option means that the index is updated with the current state of the file.
Excellent screencast... however, I am facing a little problem here.
I've got a messages controller (and a message Model), an different methods among which index, unread, flagged... etc that performs different Message.find(...) but render messages in the same way, with a "respond_to", for each type of call (HTL, XML, or AJAX).
Should I put all the logic in the partial, within the cache block? It seems pretty awkward to do a case params[:action] in my view... and keeping index unread, flagged actions empty in my controller!
Strictly speaking, you _really_ should be returning a 403 in those cases. There *is* as a resource there, it's just forbidden to that user (or role, or what have you).
The simple way to do this is Rails is:
head :forbidden
Nick Kallen has shown a nice pattern[1] for doing this type of thing consistently across your app, using Rails' rescue_action method.
In reality you might want to override rescue_action_in_public instead -- check the API docs to get more of an idea of how these methods work, like how to make sure you can still render a custom error file.
I'm able to run a script, but the files don't contain useful numbers. On Ubuntu (32bit), the first column of profile-flat contains a mix of inf and nan. I'll dig into this, but if you have some guidance, it would be greatly appreciated.
I've tried it on 32-bit Vista, 64-bit Ubuntu & 32-bit Ubuntu. They all report some sort of error. On 64-bit Ubuntu, it aborts with an Integer range error. On Vista, it complains that the files (txt & html) don't exist (though I can open them and see listings for all the methods with no timings). On 32bit Ubuntu, it complains twice about "Could not get a file descriptor referring to the console." These last two create files though the content lacks timings.
You don't specifically add the "caches_page" command to the states controller, just the "cache_sweeper" command. Is that in place of the "caches_page" command?
@Coop, Eilert Islaksen: I too was wondering how to combine authenticate_or_request_with_http_basic with the show/hide edit/delete links explained in Railscast 20.
The way I achieved this was to set an is_authenticated instance variable:
# products_controller.rb
def authenticate
@is_authenticated = authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
end
end
Then you can show/hide in the view by:
# index.haml
- if @is_authenticated
= link_to …
You could add a helper method:
# application.rb
helper_method :admin?
def admin?
@is_authenticated
end
Mourad Hammiche: instead of use formatted_categories_path(:js) you can use categories_path and at the :with clause you specify something like "partial=true" or "autocomplete=true", then you handle it in your controller to render a special HTML template for autocomplete. Imao it's better then use the js formatted being not semantic.
Hi
Your screencast make programming rails even more easy !
But there is something strange in the convention used for returning the matching categories.
"categories/index.js.erb" assume you returning javascript code ?
How does it works if I need to get real js elsewhere in my project ?
Thanks
Thaks! :P
Thanks very much - awesome!
I cannot get autotest to work on XP. I get error message "couldn't find HOME environment.." What should I add to the environment variable setting in Window?
My last comment isn't meant to say that this should've been included in the tutorial; I realize it's definitely in the realm of a separate discussion/tutorial. It's just a suggestion for those actually wanting to implement OpenID.
Everyone should be aware that there is a level of trust that needs to be established before allowing any user to login via OpenID, just as with normal authentication. Trust could be established with a CAPTCHA, whitelist, email verification or other methods.
I think with this there should be a step right after initial OpenID auth that is a CAPTCHA before the account is created to reduce the possibility of spam. This is critical for anyone adding OpenID authentication to his/her system.
OpenID is not inherently trustworthy.
So:
1. User goes to login page
2. User logs in with OpenID
3a. If it's his first time logging in redirect to a CAPTCHA and then, if successful on CAPTCHA, create the account and log the user in.
3b. If he is already on the system authenticate without the CAPTCHA and log the user in.
@Marcus - Thanx man, this info saved me quite a lot of time, thx!
@Ryan - keep on going!
Congrats on 100 Ryan! Thanks for so many fantastic videos.
Hey, love the spiels you cast. Can't get enough of them.
For those with layout issues. If you've defined a layout in application.rb for use site-wide, like application.html.haml or simple.html.erb, then just make another like application.rss.builder or simple.rss.builder (following your convention) using the following code:
xml.instruct! :xml, :version => '1.0'
xml.rss(:version => '2.0') do
xml << yield
end
Put the rest of the RSS code in your index.rss.builder view. Now you can remove the render call and keep your project DRY.
The only downside is builder doesn't indent the yield section of XML -- but that does save you two bytes per item line on output :)
Is it me, or is there a lot of stuff missing if you don't follow these tutorials in order?
I tried following along with this one (Dynamic Select Menus) and all of a sudden kept running into things that were not mentioned in the video: for example, creating the "new" action, creating the Countries and States tables in the db, modifying the routes.rb file (OK, that one was mentioned, just late in the video), etc.
It's impossible to follow along when you don't have the groundwork covered, and now I don't know where the info is that I missed.
Thank you everyone for your encouraging comments!
@Thomas, I'm using KeyCastr.
http://stephendeken.net/wiki/software/keycastr/
@DJ, there's two feeds, make sure you're using the iPod feed if you want it to play on the iPhone. :)
@Benjamin, great suggestion. I'll reply to each contest submission to confirm I received it.
@Artūras, a new object is created for each rating, yes. However, I don't think this will be a problem as each object is displayed on the screen. From my understanding, the problem with the old paginator is that each page was an object, so if you have thousands of pages you end up with thousands of objects even though not every one needs to be displayed. You will likely not need to display thousands of ratings at once.
@Jorge, good point! I considered placing this in the lib directory but decided not to because of the similarity these objects have with helpers. Either way I think it's best to make a renderer directory (which I really should have) and add it there.
@bryce, the renderer isn't a controller, so I'm not sure how inheriting from ApplicationController will benefit you. It does have some similarities in how it can render views, but the controller does so much more that this should not be concerned about. Also it's best to keep view code out of the controllers, but I feel okay to put view code in a renderer.
@Ale, I recommend moving this into a plugin if you want to share it between apps. However I find it's easy to get carried away with this and make the code more abstract than it really needs to be. Keep it specific to your application until you really see duplication with another app and then refactor that into a plugin.
@Joran, for me components often have too much overhead. I'm also not sure what benefit it has over this technique.
@James, I generally try to stay away from method_missing, especially in more complex objects when dealing with inheritance. However, the renderer seems simple enough where the downsides of method_missing aren't as apparent. In the code example I gave it's not really necessary, but if you're calling helper methods frequently in a renderer it can be very convenient.
I get this error too. Anyone know how to avoid this?
Thanks for the great screencast!
I found it useful for me, so I've made a summary of your screencast and put it in my blog. The permalink is: http://xucros.com/2008/4/18/restful-authenticationin-rails-quickly
Please, let me know if I should remove or change it.
It took me awhile to figure out what projects_path is referring to. It's a variable that is created by adding the line "map.resources :projects" to the routes.rb file. Note that :projects refers to the model name, and that it flows through to the variable. So, if the model is called "visitors", the routes.rb file would you the symbol :visitors and the variable name would be visitors_path.
I hope this helps.
Thanks for the great screen cast. I found it really helpful.
I like this method a lot - I just made one small change:
content_for(:title) { h(page_title) }
Adding the "h" method right to the helper ensures that no matter where I display it, it's safe. I often yield that title in a few other places, like in an h2 element somewhere on the page itself.
Hi,
Just a quick question, do we get a confirmation reply wen we submit our 5 tips to contest@railscasts.com? Just to make sure you really got the mail :) My mail sometimes get stuck in the spam folder :(
Thx
Thanks for the great casts Ryan.
For anyone struggling to get this approach to work for date_select or datetime_select etc as mentioned above by @joel see http://dev.rubyonrails.org/ticket/10551
Guys, I'd like to suggest going here www.rubyheroes.com and nominating someone (hint, hint) for his outstanding tutorials, delivered weekly, about Ruby on Rails. Six people will be selected to win an award on stage next month at RailsConf (which, while in my home town, I won't be able to attend this year, boo hoo!) If anyone deserves an award, it would be Ryan, don't you think?
I have a question about the method_missing part. Is it best practice to design your code around exception being thrown? The code in the method_missing part isn't unknown at the time the class is written (like the find_by methods). It seems an expensive way to get content_tag instead of @template.content_tag.
Thanks for the episodes they have been really helpful
Firstly excellent cast. But I have the same question as @jetmedia.
How to get this working with :through associations. Thanks.
Is it here or the repository for will_paginate is down ?
Check out the stencil plugin. Its basically a subclass for these types of helpers.
http://rubyforge.org/projects/stencil/
@samani ismael
Hi, the method will_paginate doesn't display the data, only handles the paging.
<% for object in @my_object do %>
<%= object.id %>
<% end %>
<%= will_paginate @my_object %>
Somehow everyone that has gotten it to work has decided to move on without sharing their discoveries. If only there were more people like Ryan
Is this not something for which you would want to use a component. I know using rails components is considered bad. But I think this problem could very well be solved with the concept of components. It would be great if rails would get a better and fast implementation for components.
What's that funky thing showing your SQL in the background?
It should be easier to share this widgets among different rails apps...
(I mean controller, images, views)
Do you know a more plug'n'play way to share these piece of code? I dunno embedded_actions, plugins, cells?
Hello Ryan your screencast are really great. They helped me to get a better rails coder :-).
Keep up the great work. Have you considerd a episode of the plugin SuperInPlaceControls for inline Edition or just some more about Ajax stuff?
Thanks for all episodes i watched them all.
Thanks for another great episode Ryan. Custom classes are still a little scary for me, but this exposure helps a lot. Thanks!
One question: rather than adding the method:
def method_missing(*args, &block)
@template.send(*args, &block)
end
could you also have the new class inheret the application controller? I.e.
class StarsRenderer < ApplicationController
I'm working on my 5 tips...
Ryan, this is your finest episode to date.
More episodes like this one! Where you get into refactoring and start touching on design patterns.
Good job kid!
Hello Ryan, thanks for this great episode. I was wondering why this StarRenderer class cannot live in lib/ directory. I use to put all my custom code in there.
Haven't tried yet but, there is a reason?
Thanks again for your time and dedication.
@Artūras Šlajus:
you may cache the results. so rails won't initialize the StarsRenderer each time.
how would you write rspec code if objects are defined from their associations like
@task = @projects.tasks.build( params[ :task ]
@tasks = @project.tasks
etc
Haven't rails default pagination helpers used this model?
Considering you (for example) need to render a rating for like... 100 products it would take 100 objects with their initializations, isn't that a bit too slow?
Another excellent episode (I see you've moved the sponsor ads to the beginning of the shows now too :( ).
I found Railscasts yesterday, and am catching up on all 101 episodes ASAP ^_^ Keep up the great work.
Have you considered a series of screencasts where you make an actual application from scratch, rather than edit an existing one. I know that for some people like myself, it would help get a grasp of which foot to start off on ;)
I know a guestbook was made in 10 minutes (there was a demo somewhere). How about building a forum ? :P I don't think there is one out there atm ^_^
You might not always want to use the -a option with commits.
I like the power of the index just having the state of the files when they were added. Using the -a option means that the index is updated with the current state of the file.
congratulations from italy and thanks for your screencasts
can u give me the code required for displaying the stack contents in the execution of the factorial program
Oh, and I forgot to say that this :
unless read_fragment(...)
@collection = Message.find(...)
end
approach deosn't do it for me... since, it messes up the respond_to.
Excellent screencast... however, I am facing a little problem here.
I've got a messages controller (and a message Model), an different methods among which index, unread, flagged... etc that performs different Message.find(...) but render messages in the same way, with a "respond_to", for each type of call (HTL, XML, or AJAX).
Should I put all the logic in the partial, within the cache block? It seems pretty awkward to do a case params[:action] in my view... and keeping index unread, flagged actions empty in my controller!
What would you do?
Congratulations from Thailand!
Happy 100th podcast!!!
Thanks a lot for those Great Podcasts,
They are Truly AWESOME.
Congratulations from Germany!
Love your podcast, they're very professional and also have a friendly touch. Keep up the good work, you sure are one of the Ruby Heroes! :)
See ya around, Dennis
@Jean and Ryan,
Strictly speaking, you _really_ should be returning a 403 in those cases. There *is* as a resource there, it's just forbidden to that user (or role, or what have you).
The simple way to do this is Rails is:
head :forbidden
Nick Kallen has shown a nice pattern[1] for doing this type of thing consistently across your app, using Rails' rescue_action method.
In reality you might want to override rescue_action_in_public instead -- check the API docs to get more of an idea of how these methods work, like how to make sure you can still render a custom error file.
Thanks for all the great Railscasts, Ryan.
[1] https://blabs.pivotallabs.com/users/nick/blog/articles/272-access-control-permissions-in-rails
I'm able to run a script, but the files don't contain useful numbers. On Ubuntu (32bit), the first column of profile-flat contains a mix of inf and nan. I'll dig into this, but if you have some guidance, it would be greatly appreciated.
I've tried it on 32-bit Vista, 64-bit Ubuntu & 32-bit Ubuntu. They all report some sort of error. On 64-bit Ubuntu, it aborts with an Integer range error. On Vista, it complains that the files (txt & html) don't exist (though I can open them and see listings for all the methods with no timings). On 32bit Ubuntu, it complains twice about "Could not get a file descriptor referring to the console." These last two create files though the content lacks timings.
Another question:
You don't specifically add the "caches_page" command to the states controller, just the "cache_sweeper" command. Is that in place of the "caches_page" command?
How would I do this for a RESTful resource? Specifically, would I just put the path in the expire_page command?
def expire_cache(article)
expire_page article_path(article)
end
@Coop, Eilert Islaksen: I too was wondering how to combine authenticate_or_request_with_http_basic with the show/hide edit/delete links explained in Railscast 20.
The way I achieved this was to set an is_authenticated instance variable:
# products_controller.rb
def authenticate
@is_authenticated = authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
end
end
Then you can show/hide in the view by:
# index.haml
- if @is_authenticated
= link_to …
You could add a helper method:
# application.rb
helper_method :admin?
def admin?
@is_authenticated
end
and go:
# index.haml
- if admin?
= link_to …
Thanks for the great content on this website. I have learnt a lot from it!