RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I have also having the problem where the params hash isn't properly created for attribues. I have the following example:
<input type='hidden' name='t[test][][1]' value='1'>
<input type='hidden' name='t[test][][2]' value='2'>
<input type='hidden' name='t[test][][3]' value='3'>
<input type='hidden' name='t[test][][1]' value='4'>
<input type='hidden' name='t[test][][2]' value='5'>
<input type='hidden' name='t[test][][3]' value='6'>
<input type='hidden' name='t[test][][1]' value='7'>
<input type='hidden' name='t[test][][2]' value='8'>
<input type='hidden' name='t[test][][3]' value='9'>

which creates the following params[:t][:test] => [{"1"=>"1"}, {"1"=>"4"}, {"1"=>"7", "2"=>"2"}, {"2"=>"5"}, {"2"=>"8", "3"=>"3"}, {"3"=>"6"}, {"3"=>"9"}]

As you can see the grouping isn't being properly assigned - i.e. params[:t][:test][0] should be { :1 => "1", :2 => "2", :3 => "3" }

Any advice at all on this is great appreciated.

Avatar

Ryan,

This has nothing to do with this Railscast, but I have a suggestion for a Railscast topic.

Would you please do a Railscast on the usage of html frames w/in Rails, particularly if the contents of those frames are dynamic. I'm not sure that this is even possible, but I would love to know for sure.

Thanks,

kevin

Avatar

excellent voice , charming episode. nice example..do well in programming,etc. U great!

Avatar

@Ryan,
If you remember, there was an issue in episode 75 in which the Project.update_attributes was not calling the task_attributes= method (comments section Andrew). I dont know if it was ever resolved(I still have that problem and would love a solution) but i was just wondering if this was the reason. You mentioned that if records were not being saved properly for some reason, this could be it. Any thoughts?
Really appreciate your work Ryan. Thanks :)

Avatar

please also mention the most obviouse choise:

link_to 'Continue', :back

Avatar

@melvin, @nick:
i completely removed daemons.yml and lib/daemons/gc_ctl.

here's my script/daemons and lib/daemons/gc.rb http://pastie.textmate.org/293636

removing options/daemons.yml solved my problem.

i know it's cheesy but works like a charm.

Avatar

Hi Ryan!
I tried to make your example work, but when i load the form, the text_field_with_auto_complete don't load the virtual attribute! Any ideas?!

Avatar

I also have the same issue as Melvin and lolcatz. Running on linux.

All I put in the daemon code were puts statements and it doesn't run. I have installed all the gems it complains about.

Avatar

@Melvin: i have the same issue...

Avatar

@Hari
you have been denied by god!
On a more serious note, check to make sure that it has +x access.

Avatar

great screencast. If only i could think of a plugin to actually write.

Avatar

I have the same problem that #66. Chris Small

Some help?

Avatar

Nice Tip :-)
Very useful little screencast again

Avatar

I followed the instructions but when I start the daemon, I get this error:

http://pastie.org/292348

Any ideas on how I might get it working?

Avatar

I really do not understand. I'm in the case of Mitchell Blankenship. Ok to named paginated pages path dynamically but how to destroy them with the sweeper ? The great way expire_fragment (% r (pages / \ d * /) notes) does not work with expire_action! It's unbelievable! I found this example that seems to work but it is inconceivable! http://www.fngtps.com/2006/01/lazy-sweeping-the-rails-page-cache You have a better method ?

Avatar

@AC, that's a cool idea. But don't you need to use an after_filter instead of a before_filter so it resets at the end of the request instead of the beginning?

Also you can make exceptions using the skip_after_filter method in the controller.

Avatar

Since the referrer is unreliable (I always turn off sending it in my browsers), I'd suggest storing request.request_uri in session instead and using that when necessary.

http://pastie.org/292092

Avatar

Another way to attack this problem is to have the User's URL store their previous route.
It's not ideal for every case, but if you have a lot of this stuff going on on your site (especially with polymorphic-like views), it can be handy as you don't have to manually keep tabs on the referer.

I've been playing around with putting this into a plugin for the last few weeks at:

http://github.com/toothygoose/historic_routes/tree/master

It's not totally weaponized yet, but I think the concept is sound.

Avatar

I'm wondering how much memory usage this approach uses. BackgroundRB is the method I've had experience with but it took like 140mb of memory to run. Any ideas on how this approach is on memory?

Avatar

In version 2.2, there's a new 'referrer' method in request.rb. This means you can simply use "request.referrer" in the controller.

Avatar

@Austin, You can use the referer technique for this, but you'll have to capture it when entering the form instead of when the form is submitted. Something like this:

<%= hidden_field_tag :referer, (params[:referer] || request.env['HTTP_REFERER']) %>

Then you can use params[:referer] in the controller to redirect back.

Avatar

Thanks Ryan for this screencast.

I have created a blog entry on how to ajaxify will_paginate thanks to lowpro:
http://hasnosorrow.wordpress.com/2008/10/13/ajaxify-wil_paginate-with-lowpro/

Hope that helps,
Guillaume

Avatar

Thanks Ryan,

great cast as always.

Avatar

first, thanks ryan!

for those who get trouble with check_box adding more than rigth rows, due to unchecked value hidden field, my work arround was:
-at the task_attributes method setter loop (at project.rb model), I changed the "tasks.build(attributes)" line, to "tasks.build(attributes) unless attributes.values.length == 1", where attributes.values.length represents the quantity of fields for each task sent by the form.
I took the idea from http://railsforum.com/viewtopic.php?id=1063

Avatar

Good episode.

What about the scenario of linking to an edit form, i.e. /products/5/edit, and after submitting it goes to /products/5/update. Redirecting back from here goes back to the edit form. I've used a few different solutions (check points, passing original_uri GET parameters, etc)

Anyone else have this issue or want to share a solution?

Avatar

@Per Velschow, the request object is documented under ActionController:AbstractRequest which you can find here:

http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000808

The "env" variables (like I'm using here) can change per request. Try tossing this in a view to see the options:

<%= debug request.env %>

@Henrik, Good point! I forgot to mention the :back option will raise an exception if the referer isn't specified.

Avatar

Ahoy,

First of all, thanks Ryan, you're still my #1 RubyHero :D

I've create a much simpler version of Ryan's example with a single select box and a list of checkboxes to store the associations.

Also uses client-side javascript and the code is less, without hacks.

Take a look at: http://clair.ro/rc/2008/10/13/has_many-with-select-box-and-checkboxes/

Avatar

Wonderful screencast. This is one of those little things everyone needs to do but not everyone knows the best approach. Thanks for continuing to do the quick simple tips between the heavy hitter subjects!
 
@Richard: Your not alone, its about the only thing to look forward to on Monday! :)

Avatar

…since redirect_to(:back) will raise if there is no refer(r)er. I don't think you said explicitly.

Trying the link again: <a href="http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g">http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g</a>

Avatar

I define a redirect_back_or_default method that uses redirect_to(:back) and ActionController::RedirectBackError: http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g

Avatar

Hi Ryan,

Thanks for your Railscasts. I am not sure if its a sign of how sad my life is at the moment but I look forward to seeing the new Railscast every monday. I used to get Crash magazine as a kid and my little heart would beat faster when I remembered that was the day that it was in the shops. Get the same feeling revery monday when I see the RSS feed, I am a newbie and much goes over my head but heh. Do I need help?

Well done & thanks.

Avatar

Thanks for these episode. Really helpful tip.

Avatar

Wanted to learn a bit more about the 'request' method/object and what you can do with it. But the docs don't have much to say about it. Do you know where it is defined and what else you can do with it?

Avatar

Useful as usual.

Good to see you still make simple Railscast suited also for Rails-beginners.

Avatar

>> Rails.cache.write('date', Date.today)
=> false

Why could this be happening?

I have memcached running and in my development.rb: config.cache_store = :mem_cache_store

All other caching works (fragment etc.) Using rails 2.1

Thanks for any help,
Justus

Avatar

When i try starting the god process i get the following error:
The server is not available (or you do not have permissions to access it)

Any ideas?

Avatar

Thank you very much for this screencast.
Your tip about working with optional arguments saved my live :-)

Really great screencast again Ryan

Avatar

Any hints on what that partial page looks like? I'm getting some newbie associations mixed up and I can't show my results. Thanks for all the screencasts.

Avatar

If anyone's interested, I've created a plugin which automagically generates common named scopes for all AR models and columns. See http://github.com/zubin/autoscope

>> Page
=> Page(id: integer, name: string, content: text, enabled: boolean, category_id: integer, created_at: datetime, updated_at: datetime)

...creates these (for example):
>> Page.enabled
>> Page.not_enabled
>> Page.named "foo"
>> Page.content_contains "bar"
>> Page.created_before 1.year.ago

Install:
./script/plugin install git://github.com/zubin/autoscope.git

Feedback welcome!

Avatar

@kirk bushell said:
"...business logic shouldn't be near the models. Imo, they are strictly for the storage and handling of data, nothing more."

The Model-View-Controller pattern is:
Model: data; business rules to manipulate data.
View: user interface.
Controller: manage communication to model of user actions.
See http://en.wikipedia.org/wiki/Model-view-controller

Business logic does belong in the model. You can only "handle" (to use your term) data according to business rules. For example, if you are displaying a record (handling data) you need to know whether the current user should be able to view that record (even if the answer is always "yes", because the site is publicly accessible). So, there's a business rule associated with handling the data.

Avatar

Tip #5 (default value for locals)

An easier way is to do something like this in your partial:

show_price = false if !defined?(show_price)

Avatar

Another nice one Ryan.

I originally used an init script to start god and load all config files under a specific directory (as per my blog post a while ago).

I'm now thinking that you could start up a god process in inittab, use respawn to keep it alive, and use the init script to load/unload the monitors.

Decisions, decisions.

Avatar

Hi Ryan, your tutorial has just saved me a few hours of searching. Thanks a lot and best wishes from Germany!

Avatar

I believe the user model should be "validates_presence_of :invitation_id, :message => "is required.", :on => :create". By doing this the model will only validate that the user has an invitation on creation rather than any time the user updates anything on there profile.

Avatar

hello, great as usual!

i have a small issue though with the following code

deal.name = Populator.words(4..9).titleize
deal.permalink = PermalinkFu.escape(deal.name)

this code dose correctly set the permalink but it also sets deal.name to be a permalink, any thoughts why??

Avatar

Interesting railscast. Does it work out of the box with pagination plugins, like will_paginate ?

Avatar

@Dnew: Try 'set :repository, "file:///Users/[path to project]/.git"'

Avatar

Nice and Helpful, just checked the screen cast seems easy to use and efficient .

Avatar

Thanks for another great railscast.

What are the advantages/disadvantages between God and monit? Anyone?

Cheers.