@Kev, I use Rails 1.2 in these screencasts, so it should work without any problems. It's hard to tell what the issue is without seeing the code. You may want to post your question on railsforum.com along with your code.
For learning I tried to improve it. Added --no-css option and default file name.
http://pastie.caboo.se/79207
Actually i want to add <title><% yield :title %></title> too. It was on one episode by Ryan before. Maybe someone can throw a link, how to modify some other file..
@Brent, good question. I recommend checking out the Rails source code to see how this is done. Go through the different ruby scripts at the URL below (particularly base.rb and commands.rb):
So how would one go about adding their own commands to Manifest so that they would rewind properly and available in the record block so I could do m.mycmd?
I'm stuck using Windoze for development and so I tried unzipping your app_layout into C:\documents and settings\myusername\.rails\generators\app_layout and it doesn't find it :(
lovin' the casts thanks Ryan. Just a small comment about manipulating DOMs using element proxies. There is one advantage of calling the DOM methods directly over page is that many DOM objects can be affect in one call like:
<code>
page.toggle :image_div, :text_div
</code>
vs.
<code>
page[:image_div].toggle
page[:text_div].toggle
</code>
I liked too much the _with_index, but when I tried to use it with a grouped instance variable: @items.each_with_index do |category, tasks, i| but it's not working :( any idea?
Ryan: Just from deprecation messages in the console: "warning: default `to_a' will be obsolete".
Googling it a bit, it seems that it's specifically Object#to_a that is/will be deprecated. Various other objects have their own #to_a (see "ri to_a") which are not deprecated.
Being unaware of the ability to sum FixNum/Float arrays I've found inject to be a great tool for that particular job.
You mentioned that you would be interested in what others are doing and most of my sums look like:
@cart.items.inject(0.0){|total,item|total+(item.price*item.quantity)}
This is also a hack, but inject is very powerful and using it in simple cases like this has helped me wrap my head around much more complicated data transitions using relatively simple injects.
Ooops, seems that the mistake that prevented me from working (which I describe in the previous comment) was all my own.
Combined a spec for checking a nil email, with a too short email and a too long email into one specification - and thats what I get, errors and comments about the wrong things.
Because :allow_nil => true was at some point moved into validates_each, so now every validation that is using validates_each (most if not all of them) now has the allow_nil property.
Even though its not in the documentation - probably for historical reasons.
Not only does validates_format_of not take the allow_nil option, it is also a known issue.
http://dev.rubyonrails.org/ticket/840
http://dev.rubyonrails.org/ticket/4208
allow_nil => true is a known method to make validations skip, to make way for validates_presence_of to kick in and put its error. But alas, it does not exist with validates_format_of ... and I have been trying to make my little rSpec pass for several days now with no success. Even the above patch did not help.
And you can find quite a bit of examples on the web where rail coders use this "missing" feature without being aware that its not there, because there is no warning or anything.
Another related problem is that rails will not convert empty strings into nil when posting forms, buts thats a whole other issue.
great stuff Ryan,
I don't see a link on the website for submitting requests, but is it possible to show how to simulate a treeview type control in Rails.
Thanks!!!
@ron, actually the controller doesn't know anything about the Category model - it only creates the Product model. The association is set by using the "create_category" method. This is provided by the belongs_to association and automatically sets the category to that product when it is created.
Could you do a part-two on this where you disallow the selection of an existing category and entering a new category name at the same time?
Perhaps adding an "other" field to the pulldown menu which dynamically makes the "create one" field fade in to the page? (and selecting an existing category again makes it fade out)
Would you please consider to do an episode that include geting totals by groups, in a way that a parameter for filtering could be pass ...
Like for producing something like :
Month No of episodes Sum of Donations($)
January 23 3,500
Febreary 5 1,000
March 0 500
.......
Total 999 9,999.99
@Justin, this method was generated from a named route which was generated by map.resources in the routes.rb file. I talk more about this in another episode:
@Mike, although you can't do that with define_method, you can put the code in a big string to begin with and then use class_eval when you want to evaluate it. That way it's in a string so you can output it however you want. See Geoff's comment above for an example.
@Mike, nope, you can only call a single method with Symbol to_proc. If you want to combine two methods like you're trying to do, I recommend creating a new "full_name" method in the actor model to do this. That way you're back to calling one method.
If you put the methods in ActiveRecord::Base they will be available to all models. You can do this through a plugin which I show in this episode:
http://railscasts.com/episodes/33
http://pastie.caboo.se/79278
Actually attr_accessor isn't needed for views :).
Banner removes just first line actually. (atleast in edge).
But maybe I'm wrong. Haven't tested it yet .
Btw, just fixed my issue - I didn't know that the environment.rb needed to be changed after an upgrade thanks for the help anyways
@Kev, I use Rails 1.2 in these screencasts, so it should work without any problems. It's hard to tell what the issue is without seeing the code. You may want to post your question on railsforum.com along with your code.
how easy would it be to add an attribute that all activerecord objects would have?
@InMan, great job! I like what you've done there! It's an excellent example of how to add options to a generator.
You may want to override the "banner" method to provide a short usage description.
@Mahmoud, I recommend asking the question on railsforum.com where you can post the full code. I'll try to reply there if I know the answer.
I don't matter, you should found me how to get rid of the <%i=0%> and the <%i+=1%> I'm having in my code! otherwise the scrrencast is useless!
No I'm joking! , your screencasts are just making of us very tough people and high class coders, You have to assume!
Good luck!
Hey guys,
been away from the code desk for a bit :)
When I upgraded rails to 1.2, should I have done anything to the app i.e. would it need to be ported?
I had already tried including:
map.resources :sessions in the routes.rb
file, but now the WEBrick won't finish booting up with the error:
undefined method `first' for :sessions:Symbol.
I'm a bit stuck and considering starting the site again using 1.2 as the starting point unless this sounds stupid.
Cheers
Great episode. That was needed :).
For learning I tried to improve it. Added --no-css option and default file name.
http://pastie.caboo.se/79207
Actually i want to add <title><% yield :title %></title> too. It was on one episode by Ryan before. Maybe someone can throw a link, how to modify some other file..
@Brent, good question. I recommend checking out the Rails source code to see how this is done. Go through the different ruby scripts at the URL below (particularly base.rb and commands.rb):
http://svn.rubyonrails.org/rails/trunk/railties/lib/rails_generator/
Another champion episode.
I've written generators before but I don't think I'd ever thought to use them for simple tasks like this.
So now, I have a database.yml generator to include my dev mysql.sock details, local password etc. So thanks for the inspiration.
So how would one go about adding their own commands to Manifest so that they would rewind properly and available in the record block so I could do m.mycmd?
Figured it out. For some reason ruby isn't using \ but is using / and so as soon as I changed USERPROFILE to use / it worked :)
I'm stuck using Windoze for development and so I tried unzipping your app_layout into C:\documents and settings\myusername\.rails\generators\app_layout and it doesn't find it :(
Any ideas on how to do this in Windoze?
@Mahmoud, each_with_index only passes two arguments into the block (the item and the index). Here you're trying to pass 3 arguments which it can't do.
lovin' the casts thanks Ryan. Just a small comment about manipulating DOMs using element proxies. There is one advantage of calling the DOM methods directly over page is that many DOM objects can be affect in one call like:
<code>
page.toggle :image_div, :text_div
</code>
vs.
<code>
page[:image_div].toggle
page[:text_div].toggle
</code>
I found this screencast extremely useful, thanks!
I liked too much the _with_index, but when I tried to use it with a grouped instance variable: @items.each_with_index do |category, tasks, i| but it's not working :( any idea?
Ryan: Just from deprecation messages in the console: "warning: default `to_a' will be obsolete".
Googling it a bit, it seems that it's specifically Object#to_a that is/will be deprecated. Various other objects have their own #to_a (see "ri to_a") which are not deprecated.
@bradc: Don't forget that you can build the current documentation locally after checking out the edge version:
> rake doc:rails
You probably want do this after you make your fix, too, so that you can make sure rdoc handled it okay.
Being unaware of the ability to sum FixNum/Float arrays I've found inject to be a great tool for that particular job.
You mentioned that you would be interested in what others are doing and most of my sums look like:
@cart.items.inject(0.0){|total,item|total+(item.price*item.quantity)}
This is also a hack, but inject is very powerful and using it in simple cases like this has helped me wrap my head around much more complicated data transitions using relatively simple injects.
Ooops, seems that the mistake that prevented me from working (which I describe in the previous comment) was all my own.
Combined a spec for checking a nil email, with a too short email and a too long email into one specification - and thats what I get, errors and comments about the wrong things.
Because :allow_nil => true was at some point moved into validates_each, so now every validation that is using validates_each (most if not all of them) now has the allow_nil property.
Even though its not in the documentation - probably for historical reasons.
@Ryan Bates
@ArthurGeek
Not only does validates_format_of not take the allow_nil option, it is also a known issue.
http://dev.rubyonrails.org/ticket/840
http://dev.rubyonrails.org/ticket/4208
allow_nil => true is a known method to make validations skip, to make way for validates_presence_of to kick in and put its error. But alas, it does not exist with validates_format_of ... and I have been trying to make my little rSpec pass for several days now with no success. Even the above patch did not help.
And you can find quite a bit of examples on the web where rail coders use this "missing" feature without being aware that its not there, because there is no warning or anything.
Another related problem is that rails will not convert empty strings into nil when posting forms, buts thats a whole other issue.
@Henrik, thanks. Out of curiosity, where did you hear that to_a was deprecated?
Continually impressed by the quality of your screencasts - thank you.
Minor niggle: I believe #to_a – you use "line_items.to_a" – is deprecated; you can do "Array(line_items)" instead.
great stuff Ryan,
I don't see a link on the website for submitting requests, but is it possible to show how to simulate a treeview type control in Rails.
Thanks!!!
@ron, build_category is like Category.new, it doesn't save it to the database. creat_category is like Category.create, it saves it to the database.
Great, Ryan. Thanks.
Could you explain the difference between build_category and create_category? The api docs don't seem to help me much.
@Ofer, thanks. Fixed.
Fixes to the source of routes.rb
1) should be map.logout for the logout line
2) probably should include:
map.resources :sessions
Which may be @kev's problem.
Just discovered your series recently - still working my way throught them - they are very insightful even on the basic topics.
@ron, actually the controller doesn't know anything about the Category model - it only creates the Product model. The association is set by using the "create_category" method. This is provided by the belongs_to association and automatically sets the category to that product when it is created.
oh... guess i'd need to see the controller for that part... wasn't thinking.
I understand how the new category is created, what I don't see is how the DVD Player is associated to the new category.
Any insight?
Hi!
Dunno if you also do audience requests, but here's mine:
I'd really like to see a screencast on backgroundrb/druby combined with rails.
great site, you rock.
You don't need AJAX voodoo...
This is just a related example, but the idea is the same from the view side.
<label for="occurs">Occurs:</label><br />
<%= f.select(:occurs, ["Once", "Daily", "Weekly"], {}, :class => 'smallTextInput', :onchange => 'Form.displayOccurs();') %>
<span id="occursbox" style="display: none;">for <%= f.text_field :quantity, :class => "tinyInput" %> <span id="timeframe"></span></span>
Form={
displayOccurs:function() {
occurs = $('event_occurs').value;
switch (occurs) {
case "Once":
$('occursbox').hide();
break;
case "Daily":
$('timeframe').update("days");
$('occursbox').show();
break;
case "Weekly":
$('timeframe').update("weeks");
$('occursbox').show();
break;
}
}
This is freaking awesome!
I have to agree that adding some AJAX voodoo to this screencast would make an excellent Part 2
Hey Ryan, great stuff as always.
Could you do a part-two on this where you disallow the selection of an existing category and entering a new category name at the same time?
Perhaps adding an "other" field to the pulldown menu which dynamically makes the "create one" field fade in to the page? (and selecting an existing category again makes it fade out)
Thanks.
Thanks again Ryan. Your tip is sure to save me some time. I'll definately be looking into 'collection_select', and it's advantages.
Thanks again
Hey, thanks. Great tip.
Tried this with tests :P.
http://pastie.caboo.se/78522
Hola !
Thanks for your screencasts, they rock !
Would you please consider to do an episode that include geting totals by groups, in a way that a parameter for filtering could be pass ...
Like for producing something like :
Month No of episodes Sum of Donations($)
January 23 3,500
Febreary 5 1,000
March 0 500
.......
Total 999 9,999.99
Gracias !
A railscast that goes into more detail on has_many :through and polymorphic associations would be greatly appreciated.
@Justin, this method was generated from a named route which was generated by map.resources in the routes.rb file. I talk more about this in another episode:
http://railscasts.com/episodes/34
In your podcast you use the function edit_episode_path(episode). What does this function link to and how does it work?
@mike, the variables in the string are evaluated, so the string contains the resulting code. See this pastie for example:
http://pastie.caboo.se/78317
Well actually with Geof's example the string is still holding the "source" / meta code.
What I want is the resulting "compiled" / generated code
Mike
@Ryan & Gary: Thanks for the tip. I didn't know that.
@Mike, although you can't do that with define_method, you can put the code in a big string to begin with and then use class_eval when you want to evaluate it. That way it's in a string so you can output it however you want. See Geoff's comment above for an example.
@Mike, nope, you can only call a single method with Symbol to_proc. If you want to combine two methods like you're trying to do, I recommend creating a new "full_name" method in the actor model to do this. That way you're back to calling one method.
@bradc, I forgot to mention this. Thanks for pointing it out. You can also find the current documentation at caboose: http://caboo.se/
Not sure why the tests aren't passing. :(
Yes, that's what I meant, i.e. access the generated code to output it anywhere.
Mike