Thanks for the screencast! Extremely helpful. I spent an extra hour goofing around because I had used attr_accessible in my model, and I needed to add category_ids to the list of accessible attributes so that update_attributes would work. Hope my pain helps somebody else.
Great series! I did have one question about the Complex Forms parts, and that is how to degrade if javascript is disabled. Is there a way to allow link_to_function to "submit" to another url so you can recreate the objects and then add a new task and render the original form or am I thinking about it all wrong?
I also had to add a custom respond to block to turn off the layout. E.g.
respond_to do |format|
format.html
format.xml { render :xml => @articles.to_xml }
format.rss { render :layout=>false}
check you have included <%= javascript_include_tag 'prototype' %> in your layout used in the edit page otherwise you need to check your $('person_country_id').observe('change', countrySelected); and ensure that person_country_id is the correct id in your edit view.
Hi i have the following problem. I followed the screen cast to create
complex forms from railscats so i create a partial view for my
equipments list, so the client can add several different equipments, I
also made another view in case looking for the equipment inside the
select box. When i choose a equipment from the view (find_equipment) i
created there is no problem when there is only one equipment inside
the client form, but when I another equipment and I try to select it
from the select view (find_equipment) it does not update the select
box it should, it update the first select box.
I have a similar problem with observe_field, it should update the
properties for a certain product, but it only update the first select
list the other lists are not updated.
Good screencast. I'd like to see more information on how you can optimize things once you narrow down the culprits. Maybe a checklist of 'gotchas' or something like that. Especially any that are common and not well known.
@Ryan, you could do it as a psychological experiment. Pavlov would be proud at the way you've got at least a couple of us trained to drool (or jump) at that sound, I'm sure.
@drozzy, the next few episodes will be a bit more advanced, but in the near future I hope to bring in some more basic ones. Thanks for the suggestion!
@Morgan, oops! I completely forgot to address this issue! Rails 2.0 uses a BufferedLogger which I'm not sure allows you to customize the format as easily. Instead you can switch back to the old logger. I *believe* this line will do it in the config file, but I haven't tested it yet.
config.logger = Logger.new(config.log_path)
Thanks for catching that.
@Vern & Carl, sorry about the intro. I'll consider changing the sound effect. Maybe a nice little squeaky toy or something. ;)
I know exactly what you mean, I always skip past the beginning, too, though it's because I'm usually at work, and I'm afraid somebody will misunderstand what I'm doing (no one here understands what I actually do anyways) and think I'm just streaming entertainment. Well, I'm sorry, but my entertainment is educational and what lets me do this 'magic' that I do all day.
Thanks first.
I've managed to make this work, but had two problems.
First, a non-related state option will display for every country, this is solved by modifying the first options.length = 1; to options.length = 0;
The other problem: On edit page, which also is using these codes, I find the saved state information is lost. The selector's default value fixs to the first option available for the country. I have no idea to fix this.
FireBug reports:
document.observe is not a function
[Break on this error] document.observe('dom:loaded', function() {
Just started using RoR a couple of weeks ago... by accident. A client moved to a new host and they offer RoR as part of the package for all customers.
I never heard of it before.
I had already started building a PHP job tracking application for the client and when I realized what RoR was... I scrapped the whole thing and started over. YEEEHAAA! This is rocking my world.
Now my brain is exploding. Had to delete all those Love Boat reruns to learn this stuff.
These Railscasts are FANTASTIC! I have one tiny small criticism... that intro scares me. I have to fast forward my DVDs when it plays that THX promo (the audience is NOT listening)... same thing here. I have to fast forward past that Railscast intro or I get anxious.
Other than that these are PERFECT and very very helpful.
I've had success using Rawk to analyze several gigs of logs. It's really nice if you have logs spread out over a number of machines. It's easy enough to cat them together and send them to Rawk. Rawk seems to be pretty fast and provides very useful output <b>without</b> the need to modify the logger.
My favorite flag is -w 20 which will give the 20 worst requests. If you have a very high traffic site these 20 worst offenders might be hogging up mongrels or your DB (-d).
@Stephen, thanks for the input. I removed Braid from the list of links for now. Looks like Git support for Piston is coming along nicely! Can't wait!
@Jörg, I agree, excellent point!
@Gavin, I believe ** will traverse the directory to multiple depth, but someone please correct me if I'm wrong.
@devon, I like to keep my schema.rb file under version control so if someone downloads the projects they can just run db:schema:load rather than running all the migrations. The comments at the top of schema.rb file recommend doing this as well.
And if I need to reset the database increment to 1 before running the fixture, how can I do it from Rails? I need that to be abble to run manuel test via the IRB like User.find(2).spouse.name, etc.
I have to say that I have learned so much watching your railscasts, it really been such a great help at my job. The only thing else i could ever ask for is more examples.. will be watching your rss feed ;)
Ryan, I love the screencasts! They have taught me an incredible amount in a short time.
I'm having a good deal of problems with this one though. I'm trying to do pretty much exactly what you have above, with one wrinkle: multiple fields for the second model.
I have an order, and items on the order. So I need to be able to setup the order (shipping address etc.) and then add items on the same form. The catch is, I need to define price, quantity and the item_id from the form. When I use your handy for loop in the model virtual attribute, I end up getting "is invalid" because I am trying to create a new record for every variable (price, qty, id). Is there any trick around this? I haven't been able to find much on virtual attributes used in the way you're using them here.
Ryan, just wondering how you call a method on the model on the remote site? For example, my host has a model called Post. The Post class has some methods called find_all_by_permalink(permalink) and find_all_by_date(year, month, day). If I try to call these methods from the Weblog controller on the client side, I get an error saying the method is undefined. How do I expose this method to my client? Or do I *have* to define a new action on the host side which does the call to Post.find_by_date for me? I've not been able to find anything on this anywhere. Thanks for your awesome casts - I hang out for them each week.
When i try and add a new client the log shows this:
Parameters: {"action"=>"new", "controller"=>"clients"}
Rendering template within layouts/clients
Rendering clients/new
Rendered entities/_entity_fields_for (0.00368)
Completed in 0.05866 (17 reqs/sec) | Rendering: 0.01921 (32%) | DB: 0.00000 (0%) | 200 OK [http://localhost/clients/new]
but nothing from the partial actually appears in the html that is generated. This is my very first attempt at using partials and I am at a loss. Is there anything obvious that I am doing wrong?
I'm a bit curious about testing. If you where to use behavior driven development, you would want to describe the behavior of your controllers.
Now I know you should not test the make_resourceful functionality because that is bound to be well tested already.
I guess I can write tests for behavior that differs from the vanilla REST setup, but then I'm just assuming that I use make_resourceful in the controller, not testing that behavior...
In the end I guess it's just about rubust code, if it does not break when you make changes and update the tests... then all is fine.
Are there someone else that have given this some thought?
I would love to see this same problem solved using Ajax. I am doing the exact same thing you show here (country -> states), however, I've put together a couple of lookup tables (using the United Nations website) that has every country, and all the "states" for each and every country.
This "state" list contains about 3500 records and I just don't think that I want to use javascript for that every single time.
So, I humbly ask if you would do the same Railscast to show the Ajax method as an alternative.
I've just tried Alastair Moore's suggestion for binding labels to the HABTM checkboxes and it works perfectly. Thanks for a great Railscast, and thanks to Alastair.
Thanks for the screencast! Extremely helpful. I spent an extra hour goofing around because I had used attr_accessible in my model, and I needed to add category_ids to the list of accessible attributes so that update_attributes would work. Hope my pain helps somebody else.
Does anyone know if there is some kind of 'safe' spam comment you can put in?
I read somewhere that viagara_test_123 was always marked as spam, but if this comment shows up it's obviously not true..
Great series! I did have one question about the Complex Forms parts, and that is how to degrade if javascript is disabled. Is there a way to allow link_to_function to "submit" to another url so you can recreate the objects and then add a new task and render the original form or am I thinking about it all wrong?
I also had to add a custom respond to block to turn off the layout. E.g.
respond_to do |format|
format.html
format.xml { render :xml => @articles.to_xml }
format.rss { render :layout=>false}
end
@Captain Zhan
check you have included <%= javascript_include_tag 'prototype' %> in your layout used in the edit page otherwise you need to check your $('person_country_id').observe('change', countrySelected); and ensure that person_country_id is the correct id in your edit view.
Hi i have the following problem. I followed the screen cast to create
complex forms from railscats so i create a partial view for my
equipments list, so the client can add several different equipments, I
also made another view in case looking for the equipment inside the
select box. When i choose a equipment from the view (find_equipment) i
created there is no problem when there is only one equipment inside
the client form, but when I another equipment and I try to select it
from the select view (find_equipment) it does not update the select
box it should, it update the first select box.
I have a similar problem with observe_field, it should update the
properties for a certain product, but it only update the first select
list the other lists are not updated.
Here is the source code:
_equipment_services.html.erb
<div class="equipment">
<% fields_for "voip[equipments_services_attributes]
[]",equipment_services do |e|%>
<p class="titulo">Información Equipos</p>
<p>
Activo Fijo
<
%=e.select("equipment_id",Equipment.find(:all,:conditions=>"estado=6",:order=>"numero_activo_fijo").collect{|
p|[p.numero_activo
_fijo, p.id]},:index=>nil)%>
<% if equipment_services.new_record? %>
<%="Estado Activo"%> <%=e.hidden_field("estado",:value=>6)
%>
<%= link_to_function "Eliminar",
"this.up('.equipment').remove()" %>
<%else%>
<=% link_to_function "Eliminar",
"mark_for_destroy(this,'.contact')" %>
<%= e.hidden_field(:id,:index=>nil) %>
<%= "Estado" %>
<%=
e.select("estado",Code.find(:first, :conditions=>["id=5"]).elements.collect{|
p| [p.descripcion,p.id]},:index=>nil) %>
<%=
e.hidden_field(:should_destroy,:index=>nil, :class=>"should_destroy")
%>
<%end%>
<%=link_to "Crear Nuevo
Equipo",add_equipment_equipment_path,:popup =>
["Agregar_Nuevo_Equipo","height=600, width=700"]%>
<%= link_to "Buscar Equipos",
{:action=>"find_equipment", :controller=>"show_select"}, :popup =>
["Show_Equipment","height=60
0, width=700"]%>
</p>
<%end %>
</div>
javascript function
function set_value(id,value)
{
opener.document.getElementById(id).value=value;
window.close();
}
find_equipment.html.erb
<table>
<tr>
<% form_tag find_equipment_show_select_path, :method=>"get" do%>
<td>Buscar Usuario:</td>
<td><%=select_tag "busqueda","<option value='1'>Modelo</
option><option value='2' selected='true'>Número Activo Fijo</op
tion><option value='3'>Número Serie</option><option
value='4'>Dirección MAC</option>"%>
<td><%=text_field_tag :search, params[:search]%></td>
<td colspan=3><%=submit_tag "Buscar", :name=>nil%></td>
<%end%>
</tr>
<tr>
<th>Modelo</th>
<th>Marca</th>
<th>Activo Fijo</th>
<th>Número de serie</th>
<th> Tipo Equipo </th>
</tr>
<%
switch=false
for equipment in @equips
%>
<tr class="<%= if switch
"TRPAR"
else
"TRIMPAR"
end %>
<% switch=!switch %>">
<td><%=equipment.modelo%></td>
<td><%=Code.getDescripcion(equipment.marca)%></td>
<td><
%=link_to_function("#{equipment.numero_activo_fijo}","set_value('voip_equipments_services_attributes__equipment_id','#{equi
pment.id}')")%></td>
<td><%=equipment.numero_serie%></td>
<td><%=Code.getDescripcion(equipment.code_id)%></td>
</tr>
<% end %>
</table>
Can anyone help me please? i'm kind of lost here
Great screencast! Thank you very much for all this great information.
Thanks
Eddie Moses
Hee i just want to give u a big thank you for your fantastic railscast! I'm looking forward to the next episodes!
Good screencast. I'd like to see more information on how you can optimize things once you narrow down the culprits. Maybe a checklist of 'gotchas' or something like that. Especially any that are common and not well known.
@Ryan, you could do it as a psychological experiment. Pavlov would be proud at the way you've got at least a couple of us trained to drool (or jump) at that sound, I'm sure.
@drozzy, the next few episodes will be a bit more advanced, but in the near future I hope to bring in some more basic ones. Thanks for the suggestion!
@Morgan, oops! I completely forgot to address this issue! Rails 2.0 uses a BufferedLogger which I'm not sure allows you to customize the format as easily. Instead you can switch back to the old logger. I *believe* this line will do it in the config file, but I haven't tested it yet.
config.logger = Logger.new(config.log_path)
Thanks for catching that.
@Vern & Carl, sorry about the intro. I'll consider changing the sound effect. Maybe a nice little squeaky toy or something. ;)
There's a pretty comprehensive git textmate bundle available:
http://blog.macromates.com/2008/git-bundle/
Make sure you set the TM_GIT textmate environment variable to the path of your git binary (/opt/local/bin/git for macports for example)
ok, got it, u have to use join model to do the build.
membership.build(:blah => Blah.new(fields))
Vern,
I know exactly what you mean, I always skip past the beginning, too, though it's because I'm usually at work, and I'm afraid somebody will misunderstand what I'm doing (no one here understands what I actually do anyways) and think I'm just streaming entertainment. Well, I'm sorry, but my entertainment is educational and what lets me do this 'magic' that I do all day.
Thanks first.
I've managed to make this work, but had two problems.
First, a non-related state option will display for every country, this is solved by modifying the first options.length = 1; to options.length = 0;
The other problem: On edit page, which also is using these codes, I find the saved state information is lost. The selector's default value fixs to the first option available for the country. I have no idea to fix this.
FireBug reports:
document.observe is not a function
[Break on this error] document.observe('dom:loaded', function() {
Ryan, thanks for the great screencasts. Does the :cache_path work for Rails 1.2.6? My proc never seems to get called.
Just started using RoR a couple of weeks ago... by accident. A client moved to a new host and they offer RoR as part of the package for all customers.
I never heard of it before.
I had already started building a PHP job tracking application for the client and when I realized what RoR was... I scrapped the whole thing and started over. YEEEHAAA! This is rocking my world.
Now my brain is exploding. Had to delete all those Love Boat reruns to learn this stuff.
These Railscasts are FANTASTIC! I have one tiny small criticism... that intro scares me. I have to fast forward my DVDs when it plays that THX promo (the audience is NOT listening)... same thing here. I have to fast forward past that Railscast intro or I get anxious.
Other than that these are PERFECT and very very helpful.
@svend:
I am working on a rails 1.2.6 app that does not use RESTful routing. To get this to work for me, I added the following to routes.rb:
map.connect ':controller/:action.:format'
hey,
I like this way of saving related objects.
How can I alter this to work with
has_many :through relation(join table)?
Right now, it blows up because of " Both records must have an id in order to create the has_many :through record associating them. "
It seems that the parent object needs to be saved before the build can work correctly.
I've had success using Rawk to analyze several gigs of logs. It's really nice if you have logs spread out over a number of machines. It's easy enough to cat them together and send them to Rawk. Rawk seems to be pretty fast and provides very useful output <b>without</b> the need to modify the logger.
My favorite flag is -w 20 which will give the 20 worst requests. If you have a very high traffic site these 20 worst offenders might be hogging up mongrels or your DB (-d).
Waiting for the follow up to this.
@Stephen, thanks for the input. I removed Braid from the list of links for now. Looks like Git support for Piston is coming along nicely! Can't wait!
@Jörg, I agree, excellent point!
@Gavin, I believe ** will traverse the directory to multiple depth, but someone please correct me if I'm wrong.
@devon, I like to keep my schema.rb file under version control so if someone downloads the projects they can just run db:schema:load rather than running all the migrations. The comments at the top of schema.rb file recommend doing this as well.
And if I need to reset the database increment to 1 before running the fixture, how can I do it from Rails? I need that to be abble to run manuel test via the IRB like User.find(2).spouse.name, etc.
I have to say that I have learned so much watching your railscasts, it really been such a great help at my job. The only thing else i could ever ask for is more examples.. will be watching your rss feed ;)
Should we add schema.rb in the .gitignore?
My .gitignore contains:
db/*
!db/migrate
and also
public/cache/**/*
@Nicolás: how is that different to something/*/* ?
Gav
Ryan, I love the screencasts! They have taught me an incredible amount in a short time.
I'm having a good deal of problems with this one though. I'm trying to do pretty much exactly what you have above, with one wrinkle: multiple fields for the second model.
I have an order, and items on the order. So I need to be able to setup the order (shipping address etc.) and then add items on the same form. The catch is, I need to define price, quantity and the item_id from the form. When I use your handy for loop in the model virtual attribute, I end up getting "is invalid" because I am trying to create a new record for every variable (price, qty, id). Is there any trick around this? I haven't been able to find much on virtual attributes used in the way you're using them here.
Ryan, just wondering how you call a method on the model on the remote site? For example, my host has a model called Post. The Post class has some methods called find_all_by_permalink(permalink) and find_all_by_date(year, month, day). If I try to call these methods from the Weblog controller on the client side, I get an error saying the method is undefined. How do I expose this method to my client? Or do I *have* to define a new action on the host side which does the call to Post.find_by_date for me? I've not been able to find anything on this anywhere. Thanks for your awesome casts - I hang out for them each week.
I'm dealing with the same MySQL database error as Klian, above. My join table only has the two _id columns, though.
Looks very interesting, am looking forward to the next episodes.
One thing though, how exactly do you get the log to show this pid's?
I cannot get my 2.0.2 to alter the log messages, and thus cannot group by controller actions.
Perhaps a sample environment.rb and a sample log would clear it up for people as thick as me ;-)
Ruby and Hallison: You need to use rails edge (or 2.0) for that. Pay attention, Ryan says that in the cast!
i really wish you'd do more casts on some more basic things, they were great! ... these things are way over my head ;-(
But maybe it's just me.
Félicitations pour votre formidable travail, à la fois pertinant et très utile.
thx.
Hi,
really nice and handy, waiting for more ;-)
This is also very nice to have it as a reference.
http://zrusin.blogspot.com/2007/09/git-cheat-sheet.html
Great episode! I liked the twist :)
Great screencast.
But I get this error:
Routing Error
no route found to match "/reporting/index.pdf" with {:method=>:get}
Can you help me?
regards
svend
Thanks a lot!
A note to those using Autotest: you might want to add an exception to ignore your '.git' folder, lest you wish Autotest to slow to a halt.
See http://blog.davidchelimsky.net/articles/2008/01/15/rspec-1-1-2-and-zentest-3-8-0
I left off the = from the opening <% of the render call. Sigh.
I extracted the following from a new.html.erb template:
<h3> Entity Header </h3>
<% fields_for(@entity) do |e| %>
<fieldset>
<p>
<b>Entity Name</b><br />
<%= e.text_field :entity_name %>
</p>
<p>
<b>Entity Legal Name</b><br />
<%= e.text_field :entity_legal_name %>
</p>
<p>
<b>Entity Legal Form</b><br />
<%= e.text_field :entity_legal_form %>
</p>
</fieldset>
<% end %>
and put this in its place:
<% form_for(@client) do |f| %>
<% render :partial => "entities/entity_fields_for", :object => @entity %>
When i try and add a new client the log shows this:
Parameters: {"action"=>"new", "controller"=>"clients"}
Rendering template within layouts/clients
Rendering clients/new
Rendered entities/_entity_fields_for (0.00368)
Completed in 0.05866 (17 reqs/sec) | Rendering: 0.01921 (32%) | DB: 0.00000 (0%) | 200 OK [http://localhost/clients/new]
but nothing from the partial actually appears in the html that is generated. This is my very first attempt at using partials and I am at a loss. Is there anything obvious that I am doing wrong?
I'm a bit curious about testing. If you where to use behavior driven development, you would want to describe the behavior of your controllers.
Now I know you should not test the make_resourceful functionality because that is bound to be well tested already.
I guess I can write tests for behavior that differs from the vanilla REST setup, but then I'm just assuming that I use make_resourceful in the controller, not testing that behavior...
In the end I guess it's just about rubust code, if it does not break when you make changes and update the tests... then all is fine.
Are there someone else that have given this some thought?
Thanks for another great screencasts. All of them have been very helpful and timesaving to me and my friends studying rails!
There's git support for script/generate in edge.
http://dev.rubyonrails.org/changeset/8772
e.g. script/generate -g
Cheers,
Rajesh.
I am having the same issue as Harlan Crystal I am using Radio buttons but they are being all grouped into one big group
What is the best way and is there a best way to call a "content_for" between
<%=yield :mycontent %>
and
<%=@content_for_mycontent %>
Interesting, as usual.
I would love to see this same problem solved using Ajax. I am doing the exact same thing you show here (country -> states), however, I've put together a couple of lookup tables (using the United Nations website) that has every country, and all the "states" for each and every country.
This "state" list contains about 3500 records and I just don't think that I want to use javascript for that every single time.
So, I humbly ask if you would do the same Railscast to show the Ajax method as an alternative.
Thanks!
B.
I've just tried Alastair Moore's suggestion for binding labels to the HABTM checkboxes and it works perfectly. Thanks for a great Railscast, and thanks to Alastair.
Git rocks!
as well as Linux and Linus Torvalds ;)
thanks for this screencast
Greate screencast. I want to quickly learn Rails ? So I need a help. Please contact me ?
I had an related problem but restarting ferret_server did help for me.