Please read for an updated status on RailsCasts:
Learn more or hide this
It will be great to use a before_filter fo @product = Product.find(params[:id]) to avoid duplicated lines.
Your screencast is a good example to show interest of using before_filter because you need to modify find to find_by_permalink in a bunch of actions instead of modifing one before_filter.
I don't know why rails generator do not generate this before_filter. (even in the views, it does not generate a _form partial but duplicates it in new and edit rhtml file) May be an idea for a new screencast if it's not already done :)
Thanks for this video!!
I didn't seen this screencast yet but anyway can't we change the "id" behaviour acting on routes.rb? It is only a route issue if the last part of the url goes in params[:id] we can change that by adding a new custom route.
Just recently I made a plugin to do exactly this: http://www.bitbutter.com/seo_urls-plugin-making-show-pages-more-findable/14
Does Rails automatically populate the permalink field? Most permalink implementations I've seen ask you to prepopulate the permalink column.
Rich - you can use Rick Olson's permalink_fu plugin to automatically create permalinks for you. You'll want to use PermalinkFu.escape(...) in a before_save however instead of the automatic "has_permalink" as the latter doesn't update the permalink when you edit a model.
@seb, good call. A before filter would work really well here, I should have mentioned it.
@Skyblaze, yes, you can add a custom route to do this as well. I don't know of an easy way to do this when using map.resources however.
I've writen a permalink plugin that extends String to allow permalizing of just about any type of string, including unicode.
Well, another approach would be to override the find method with something like:
return find_by_permalink(*args) if !args.first =~ /^\d+$/
Or something similar, but You'd need to validate your product permalinks to not be all numbers, or else things can get messy. Still, could solve problems when you are adding this to an already existing app.
If you are doing this for a new app, then there shouldn't be much to worry about.
Oh, and of course, nothing beats having good tests to make sure no find method goes wild ;)
Great stuff, Ryan. It's really humbling the service you're doing to our community.
More, more, more RESTful stuff, please. This is the future.
Thanks for wonderful work that you are doing in the Rails community. I am just getting aquatinted with Rails and Ruby in general, and have completely gone nuts over the fact that it's wonderful to code in Rails and fun too! I think I am become a Rails addict!
I have been watching some of the most wonderful pod-cast on iTunes by you and have equally become and addict their too:-).
I have a question with regard to generating a view in which multiple controllers can be called into it. Instead of having only one model being displayed in the view, can I have multiple models, through one of the controllers, in one view? I am talking about the way one can generate a query in the database that then become another table. I am not sure if I am making sense, but very very much appreciate your response and a take on this.
Thanks so much,
I had to do something like this for an admin page where people could see what employees had worked on on a specific day, but I needed to be able to use either an id number or username (it's an internal website for logging people's work), so I had the controller do a regex on the param. If it was all digits, then it must be an id, of it's not it must be a username. Of course I only had one controller I had to do that in, so it would probably not be a good idea in other areas.
Yet another plugin.
See Dan Manges post on Taking DRY Too Far
@Alek, I'm not entirely sure what you're asking. A controller can fetch as many models as it needs in one action/view. However, if you want to apply the technique showed in this episode, you will need a name to use in the URL. In that case the focus will be on one model, not on multiple models.
Thanks very much for responding to my question. I am sorry that my question sounded somewhat confusing.
Here is what I really had in mind from the question I posed. Just as you would create queries within database, by merging tables into one entity, can you do the same in Rails?
You can use the ":include" option in the find method to include multiple tables/models in one query.
If you're trying to make one model span multiple tables then I don't think that's possible without serious hacking.
Yet another permalink plugin!
This one raises RecordNotFound in case given permalink doesn't exist, so you can use it like #find and catch not found errors in rescue_action_in_public
Thanks Ryan! Your info helps a lot. As for having one model spanning multiple tables, it is something I will not commit, even though it can be done, just because of "serious hacking" as you say. The :include option sounds perfect, however! Thanks again.
Note that you DON'T want to use "_" as the separator between the id and the permalink, if you rely on String#to_i. The screencast uses "-" which is fine.
If you use "_" and the permalink part begins with a number, you could get e.g. "123_456blah", and "123_456blah".to_i is 123456, not the expected 123. This is because "_" can be used in Ruby to make numbers more readable.
Found this on the net:
You don't have to make a new field in your table, yu could just use this instead:
replace name with the field you want to slug/permalink.
What do you think Ryan?
Im confused a little bit. I created new column in my db called permalink. Then i did like in the video, but my urls are /5-
no permalink string. I know the problem, i dont have anything in permalink. Is this permalink generated automatically from somewhere? Or do i have to do it manually, when i create new ex. article?
It works. But does this work with paginate plugin as well?
This has to be my favourite till now!
Thank you so much i really didn't know how to accomplish that (it is my first rails (and web development in general) project!)
Mo Rashed: Thanks for the tip! I actually have :
Having .html makes it more search engine friendly.
Very cool, i think i've to change a lot of stuff. i've done a really bad thing (the permalink contains all the id-permalink stuff, and then i changed the routes.rb to use it instead of the id... a really bad thing :) )
now how would I format this route:
This tutorial just saved me even MORE time. I was missing the def to_param in the user model. I was about to start messing with the routes, thankfully this is a much cleaner way to get this done.
Some of these things should be easier done. Oh well. Thank you!
I thought for sure a permalink set as "1-epson-abcd-1234" would fail, but sure enough, to_i still converts it to 1.
I must admit I'm not entirely certain why or how, but it does. I will have to look into this.
Any suggestions on how to allow permalink's with periods (".") in them? Assuming product is routed using "map.resources :products", then anything after a period will be treated as a format.
I have a small question. I've defined the "to_param" method but when I acess to /projects/1 (typing the URL) it doesn't change to /projects/1-name.
Here at railscasts it does changes the /episodes/63 to /episodes/63-name... how do you get that?
LOL @ Settlers of Catan... thats a fun game. You screencasts are invaluable!
Thank you! Once again you have saved my project from failure! I've learned 90% of my knowledge about rails from watching your screencasts. Please don't stop :)
Hi! Google gerade im Web und bin auf dieser Homepage gelandet.
Respekt, haben Sie toll gemacht.
Ich wünsche Ihnen weiterhin viel Erfolg mit Ihre Seite und natürlich viele Besucher.
Hier habe ich mich sehr wohl gefühlt und werde wieder hierher zurückkommen.
Freue mich auf einen Gegenbesuch auf meiner Homepage
I second @Nicolás Hock question. How to redirect from /episodes/63 to /episodes/63-name.. in order to avoid caching duplicate pages.
Thank you for this screencast!
But what if I have Page model
class Page < ActiveRecord::Base
has_many :subpages, :dependent => :destroy
And I have Subpage model
class Subpage < ActiveRecord::Base
How may I use this method for urls like?
I've found only this way for routes.rb:
match 'ru/:page_id/:id' => 'subpages#show'
Hi there, how can i get load_and_authorize_resource working with this from cancan?
This episode is updated for Rails 5 as a blog post Model Name in URL
tube besi m baja dari Lasbaja batas https://info.schreiner.edu/ICS/My_Pages/Bookmarks_594.jnz 2711 besi quonset efektif baja amanbaja menawarkan https://info.schreiner.edu/ICS/My_Pages/Bookmarks_595.jnz baja resistansi blok 2dingin selesaibaja sebuah besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_596.jnz lain baja besi pakai pipabaja rasio merah https://info.schreiner.edu/ICS/My_Pages/Bookmarks_597.jnz besi produksibaja pikiran diterima lembar baja sama https://info.schreiner.edu/ICS/My_Pages/Bookmarks_598.jnz aplikasi baja 3tinggi rendah pertama cepatbaja besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_599.jnz baja besi parkir kaki langitbaja permintaan satu https://info.schreiner.edu/ICS/My_Pages/Bookmarks_600.jnz baja grinding diikat dijual mahir akhir besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_601.jnz besi pakai hanggar baja baikbaja resistansi bukan https://info.schreiner.edu/ICS/My_Pages/Bookmarks_602.jnz baja atas tiga ratusan lengkunganbaja sama besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_603.jnz baja bagi menarik ketebalan tersebut membentuk besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_604.jnz baja sisi panasbaja ibukota penurunan ini besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_605.jnz besi orangorang cvd harus produksi baja mengangkat https://info.schreiner.edu/ICS/My_Pages/Bookmarks_606.jnz db6 dibuat jenis ke panas baja besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_607.jnz besi dibangun aus hampir baja mampu atau https://info.schreiner.edu/ICS/My_Pages/Bookmarks_608.jnz
besi tebal membungkuk titanium relatif baja efektif https://info.schreiner.edu/ICS/My_Pages/Bookmarks_609.jnz rating tentang grinding 2dingin baja besi kolom https://info.schreiner.edu/ICS/My_Pages/Bookmarks_610.jnz bahan besi manfaat baja konduktivitas dunia batas https://info.schreiner.edu/ICS/My_Pages/Bookmarks_611.jnz besi kekuatan langitbaja berbantuan ekspansi 5plastik baja https://info.schreiner.edu/ICS/My_Pages/Bookmarks_612.jnz besi ide lain baja oksida tahap ofaircraft https://info.schreiner.edu/ICS/My_Pages/Bookmarks_613.jnz ini baja relatif pilihan menjelaskan modulus besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_614.jnz Sinar unik baja listrikresistensi besi pencakar melahirkan https://info.schreiner.edu/ICS/My_Pages/Bookmarks_615.jnz baja besi memiliki kecepatan stabilitas dingin density https://info.schreiner.edu/ICS/My_Pages/Bookmarks_616.jnz pukulan baja disebut grinding besi pukulan dari https://info.schreiner.edu/ICS/My_Pages/Bookmarks_617.jnz set baja alat kelimpahan bingkai quonset besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_618.jnz proses memilih kimia baja beberapa maka besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_619.jnz besi baja sejumlah diikat melalui kerugian merupakan https://info.schreiner.edu/ICS/My_Pages/Bookmarks_620.jnz berakhir besi baja akhir bingkai paling melalui https://info.schreiner.edu/ICS/My_Pages/Bookmarks_621.jnz besi baja berdasarkan bahwa hidupbaja pikiran ibeambaja https://info.schreiner.edu/ICS/My_Pages/Bookmarks_622.jnz ya! baja tethers modulus stabilitas cairan besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_623.jnz
baja membutuhkan besi pangkalan perang rating jarak https://info.schreiner.edu/ICS/My_Pages/Bookmarks_624.jnz sehubungan besi berakhir baja pembangunan proses menjadi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_625.jnz memutuskan pesawat baja tertinggi besi kegunaaninjeksi besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_626.jnz baja pembangunan disediakan datar disampaikan integral besi https://info.schreiner.edu/ICS/My_Pages/Bookmarks_627.jnz besi karena baja energi Sekarang rhode Lasbaja https://info.schreiner.edu/ICS/My_Pages/Bookmarks_628.jnz besi cairan pembangunan fisik baja mesin kirakira https://info.schreiner.edu/ICS/My_Pages/Bookmarks_629.jnz besi baja Hal analisis harga swaging baja https://info.schreiner.edu/ICS/My_Pages/Bookmarks_630.jnz baja elastisitas pvdbaja menahanbaja dibandingkan pameran besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_487.jnz besi lain baja diterima digunakan pakai diikat https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_488.jnz besi baja mati? luar ferrule dipangkas pasif https://my.uttc.edu/ICS/My_Pages/Bookmarks_560.jnz besi tegak baja komputer cor tekanan pakai https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_490.jnz besi daripada akan mandrelbaja dikenal baja luar https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_491.jnz tanpa aus besi seluruh memerlukan baja termal https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_492.jnz besi baja tanpa kayu sadar salah pengelasanbaja https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_493.jnz dijual kirakira baja pada besi ratusan berbantuan https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_494.jnz
melewati baja diingatbaja sektor melibatkan selesai besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_495.jnz besi sadar baja ketebalan masa melihat manufaktur https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_496.jnz rendah baja adabaja pencakar semacam penempatan besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_497.jnz ibeambaja sebagian lain pasif baja dapat besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_498.jnz besi bahwa mencatat machinability baja mengalamibaja jenis https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_499.jnz machinability hobi baja lembar biasa tiga besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_500.jnz besi tikungan 2711 baja dikonsumsi daktilitas populer https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_501.jnz baik memotong cvd huruf setelah baja besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_502.jnz besi berubah perang baja Anda diakui tekanan https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_503.jnz tempat baja kebutuhan besi gasbaja halusbaja perusahaan https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_504.jnz baja menahan berkontur tahan m diameter besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_505.jnz baja korosi tertarik akhirnya demikian aspek besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_506.jnz baja ide sifat vakum seorang mungkin besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_507.jnz baja lebar membutuhkan hanya komputer saat besi https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_508.jnz besi saat paduan baja terawal jugabaja adalah https://umportal.umobile.edu/ICS/My_Pages/Bookmarks_509.jnz
First sign in through GitHub to post a comment.