RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

@Olivier, hmm I'm not sure what the problem is without seeing more code. But here is not the best place to post the code. Instead I recommend starting a thread over at railsforum.com with a detailed explenation of your problem. I frequently visit that forum so I'll probably be able to reply there. Thanks. :)

Avatar

Ryan,
This was a very useful episode. Even though I am an experienced SVN user. I never really had a good process for Rails projects. This was great! To add a quick suggestion, I think you should continue to not assume that people know things. The more that's explained the better.

Avatar

Hey Ryan. Great screen casts! I've watched them all, and have recommended them to all my Rails developers.

Anyway, I thought it would be a great series of screen casts to talk through using RESTful designs for those non-obvious resources, like you mentioned in this screen cast. Such as, the "completed" resource. Maybe, a simple one would be to use Users and Sessions as an example of RESTful User login.

Avatar

I have just one small suggestion.

When you have Ruby wrap the block into an object for you (by adding a &block parameter), I think it's more consistent to use block.call over yield. Generally I thin you either want to go with a yield/block_given? strategy or a &block/block.call/block.nil? strategy, depending on the method you are writing. But it's probably best not to mix and match. In your examples, we need the block object to get the binding, so the latter is better.

Avatar

Wildly useful and solves a small problem I had with my views. Thanks, Ryan!

Avatar

Hi Ryan,
As you tell me, I try to use :field_name instead of "field_name", but dont work... Moreover, this feature dont work only with id fields. Fo you have another idea ?

Thanks

Avatar

Good example!
I google about "Variable Bindings in Ruby"
http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc
and,with your example,I understand it
tks!

Avatar

@niko, right. This isn't as necessary in edge rails since the query is cached. However it will return a new instance of User every time so I still recommend caching it in an instance variable.

Avatar

This is kind of automatically included in edge rails auto query caching.

Edited by moderator. July 3, 2011

Avatar

It would be great to show an example of how to mark an associated label with an asterisk or something similar. I think this is used a lot to mark a field as "required" i.e. instead of having a red border around the field, you would mark the label shown above the field either in read or with an red asterisk.

How would I do that?

Avatar

Thanks Ryan. I wanted to learn how to do that just a few days ago and this railscast will save me a lot of time!
I'm a big fan and would not miss an episode! Keep up the great job.

Avatar

Hmm, if the field name you provide matches the name used in the field itself, it should work. You might want to try making it a symbol.

errors.add(:field_name, ...)

Avatar

I do:

<title><%= h @page_title || "Set title in: views/#{params[:controller]}/#{params[:action]}.rhtml" %></title>

Avatar

Hi Ryan, thanks for this new screencast. About this error validation tips, I have one question. Sometimes when I manualy add an error action in a validation method in the model, nothing happend on the field.

IE:
def validate
  if a_special_check
    errors.add("field_name", "my message...")
    return false
end

Any idea ?

Avatar

@Chris, I beleive this is possible through instance_tag. You can access the model through that in addition to the errors and the name of the attribute the field represents. The pieces are all there, you just have to figure out how to put them together.

Unfortunately instance_tag (ActionView::Helpers::InstanceTag) is poorly documented. You have to read the source code.

Avatar

I am just wondering if there is a way to wrap the error that corresponds to the input control within the span block. It is always nicer to allow the user to see the error message right beside the input control rather than a summary of all the errors at the top of the page.

Thanks for the videos, I always look forward to them.

Avatar

Usefull tutorial. I haven't commented any screencasts on this page before but I've seen all of them - thanks! You're doing great job for rails community :)

Avatar

Ryan,

You rock. I have been a big fan of yours through both your amazing posts at railsforum.com and these railscasts.

Keep up the good work. All the best.

Avatar

@Martin, I don't know if this feature is documented in the rails api. I haven't seen it anyway.

@Erik, there's a snippet on this page which does that:
http://snippets.dzone.com/tag/field_error_proc

I think it would be cleaner to change an option in instance_tag and have it regenerate the HTML. You just have to watch out for the seamless loop because regenerating the HTML calls the proc again.

@Jim, good suggestion. An episode about Rake is planned, just need to sit down and record it some day. :)

Avatar

These RailsCasts are so great - I am speechless! A BIG THANK YOU!
Greeting from Germany,
Martin

Avatar

Thanks here too. I"ve wasted a lot of time trying to make my forms look good when errors are displayed, this allows me to get rid of a bunch of hacky code.

Avatar

Nice job!

I know you get tons of requests for screencasts but I'd actually like to see something small about Rake. Obviously you couldn't cover all of Rake in 3-4 minutes, but it might be instructive to see a small task or two that you've written that find yourself using frequently.

Avatar

Hmm... What if I don't want another wrapping tag but just want to set the class attribute of the input field, then how would I do?

Avatar

Already used that :).
"<span class=\"fieldWithErrors\">#{html_tag}</span>"
Thanks anyway.

Avatar

Hi Ryan, thanks for this (and all) screencast. About this one, where can we find the reference of ActionView? I think this documentation will be usefull for rewrite the functions.

Regards.

Avatar

If people are having problems getting subversion started you can use it locally without any server running by using the file:// protocol.

Here are a list of steps to create a new repository and add something to it.
http://pastie.caboo.se/66779

Avatar

Thank you very much for this great podcast. And it made some things a lot easier, I already started to something like this /projects;search thingy and so on. But this one is much easier...

Avatar

Another great railscast! please keep them coming!

Avatar

One way round the AJAX form problem is to have one submit button, and use a "preview" checkbox.

Avatar

flash[:error] does not work unless it is matched in the view layout, just in case anyone has issues getting the error message to show

Avatar

Thanks Ryan -- these screencasts are teaching me how to think in a rails way. Embarrassed to say that I have heard your voice in my mind when I'm coding my own project.

Avatar

I'm agree with the first comentary ;)

Avatar

Oh thanks! It was the last thing you deleted on the model... I must have blinked!

Avatar

This is too funny. I was halfway through a post on railsforum asking how to do exactly this when my RSS alerted me to a new Railscast. Your timing could not be better!

d:)

Avatar

@David, check your model. It looks like you are still referencing "params" in there. Notice I removed the "params" reference when moving it to the model.

Avatar

Okay, so I'm working through this screencast and I'm getting some errors... basically, everything looks okay and I can do a 'blank' search, but once I put something in it, I get the following:

undefined local variable or method `params' for Company:Class

#{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/base.rb:1233:in `method_missing'
#{RAILS_ROOT}/app/models/company.rb:13:in `search'
#{RAILS_ROOT}/app/controllers/companies_controller.rb:5:in `index'

All my code is in the same places (and looks the same) as yours does in the tutorial. Help?

Also, how would I go about writing tests for this if I was to do this (add this functionality) in a TDD way?

Avatar

Yet another great screencast Ryan!

I just finished Peepcode's TDD screencast and I have to say, wow. Perhaps you can do some heavy duty TDD episodes in the future?

Avatar

I've been doing index() searches like this for a while now, and it never occurred to me to set the value of the text_field to the params, to make repeated searches nicer. Great idea, and it just went into my app. Thanks!

Avatar

I'll try to work on adding code snippets, notes, and links/resources to the episodes. Also thanks for the suggestions for future episodes everyone. :)

Avatar

@Sydney, I can't answer everything, but I'll try to tackle a few of your questions.

Although I used "localhost" in this episode, you should use whatever server you are hosting the repository on. This is probably your web server. Rarely do you host the repository on your local, and I don't recommend it for a real project since it will make deployment more difficult. I just have a repository set up on my local which I use for examples and testing.

While it would be nice to go more in depth and provide a complete beginners screencast, doing so on a topic like this is near impossible. That is because it is different for almost everyone. Some web hosts allow you to easily setup a repository through an admin interface, some you have to do it from scratch. Also the installation varies depending upon your platform.

After you get the repository setup and subversion installed you should be able to follow along with the screencast. I only covered the parts which are Rails specific to keep these screencasts on topic.

Avatar

@Ben and jeroen, right, I should have mentioned this in the episode. I may revisit this in a future episode because I think it's a pretty important point.

Avatar

Yep, you can treat it just like a normal attribute and add a validation like this:

--
def validate
  validate.errors.add(:full_name, 'must have a space') unless full_name.include? ' '
end
--

However, the problem is the getter method may not represent exactly how the user typed it. So it's best to store the user submitted data in an instance variable so you can return it in the getter method when it's set.

Avatar

Loving the simplicity of this screencast. I hope you do a part 2 where the user can select from a dropdown menu to filter results by different fields.

PS - I have to second Manuel's idea. Snippets would totally rock.

Avatar

Nice one! One idea: While coding I often think: Haven't you seen a clever hack for this in some recent railscast episode? So I find myself loading the movie again just to lookup some code snippet. What about adding the source snippets to the episode page, maybe folded to avoid clutter...?

Avatar

Very nice!

When the full_name has no spaces, the first_name and last_name will become equal! Is it possible to add an extra validation rule onto the virtual attribute?

Avatar

Great espisode!

I'm pretty sure it's better to add a specialzed controller for this "CompletedTasksController" which has the default REST actions. DHH also mentiod something simlar in his World of Resources presentation.

Avatar

Hey Ryan, these screencasts are excellent. However, as is often the case with anything to do with Rails, this screencast assumes an awful lot and this means, for newbies like me, it can create as many questions as it answers.

Some of the issues I experienced are:
- subversion on my computer doesnt work with localhost. perhaps this requires some additional setup steps which i havent got to in the manual yet.
- my rails project has all the rails stuff in the same directory. should it? there is a huge vendor directory which is now in the repository but i am not sure that is right.
- i dont really understand whether my repository should be on my notebook or on my webserver. or both? do the two repositories interact?
- where should subversion be installed in the first place in a rails development environment? or doesnt it matter?

I printed out a 330+ page manual entitled "Version Control with Subversion". It is quite a nice manual, but its not Rails-centric. Still, I am assuming this is the manual you would recommend to anyone installing Subversion for the first time?

If so, what would be very helpful, is if your screencast (or a new one) dovetailed in with this manual, so that the newbie user can go through the installation and setup process but then turn to the screencast at the appropriate point.

For example: on page 16 of the manual there is a section entitled "getting data into your repository" but because I knew there was a railscast i decided to use your method, being rails-centric. But, the directory structure you are using doesnt really seem to fit in with what the manual suggests, and of course there is no localhost support at this point of the installation.

It seems to me that much of the information available on Rails (including nearly all the books) are aimed at an audience familiar with command line unix, and object oriented programming techniques. This makes it very difficult for anyone to string together all the things they need to know.

While I know it is not possible to include everything in a railscast, i think it would be good to keep in mind that many people coming here will be new to one or other part of this environment - and maybe, like me, new to all of it. I hear much about people moving to Linux/Unix/Mac and Rails, but when we get here we find a community which ignores the needs of the newbies!

If you think about a new Rails developer, then the environment of Apache/Mongrel/Capistrano/Subversion etc is quite a lot to contend with - and necessary, it seems, in order to deploy a Rails application.

There is no need to include everything in one railscasts as long as you can link in with manuals etc which are already out there, and make it clear where your stuff hooks in to that.

:-)

Avatar

Check out my simple search plugin that adds similar functionality to a model.

http://svn.joshpeek.com/projects/plugins/searchable/

Avatar

Ok, so how would you go about creating a RESTful controller while not creating the model.. I'm brand new to REST, and the only way I know of generating a resource is scaffold_resource? Any tips?