RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

The problem seems to be solved now. Great screencast, Ryan!

Avatar

Many thanks Ryan, you're our hero !!!

Avatar

No problems at all here.

Great 'cast Ryan - as a bonus you may want to add a link to Ben Mabey's presentation at MountainWest RubyConf

http://mwrc2009.confreaks.com/14-mar-2009-15-00-bdd-with-cucumber-ben-mabey.html

^^ I like how he breaks everything down

Avatar

I'm unable to download the screencasts, too. Seems that the media server is down.

Avatar

All screencasts aren't available, looks like the host is down

Avatar

Yes, I cannot download screencast too...

Avatar

same here: 503 Service Unavailable

Avatar

@Gavin, Yes, me too! Initially I thought it might be a local problem or perhaps i was trying to pull it a bit too soon but it seems like something is missing on Ryan's end.

@Ryan, waiting patiently.

Cheers,
Aditya

Avatar

Hi Ryan,

I think there is a problem with the screencast today, I don't seem to be able to download or view it. Nobody else has commented, so I thought I better :-)

Avatar

Hi Ryan.
First thanks for a great episode.

Is it possible to create a nested form building children model who themselves have a has_many relationship with their own children and successfully implemented this on the one form. Further to this, I am trying to implement this using ajax and wondered (again: if this is even possible?) how to pass the formbuilder object back to my partial.

One other quick one: I noticed that to date their seems to be only javascript workarounds when adding more than one child in the 2.3.2 accepts_nested_attributes_for. Are you aware of any work/changes pending to resolve this?

Appreciate all your work
Michael

Avatar

just for note. I did get this to work in 2.3.2
Check for misspellings!

Avatar

@Erik

Ugly but works:

redirect_to @commentable.class.new

Avatar

Ryan, thanks for that as always.

I've Been putting off moving over to 2.3 for a while now, but this convinced me that rack is the perfect solution for a problem I've been seeing with cookieless sessions whereby some phones won't send get and post parameters as part of the same request.

Avatar

Hi,

I was searching for a way to split an ActiveRecord attribute in view (i.e. phone number = country code + area code + number)

I found the following post, looks promising. Although I could not get it working. A railscast would be greatly appriciated on it.

http://blogs.entertonement.com/nerdery/2008/07/rails-multipara.html

Thanks

Avatar

Thanks for the great tips Ryan. I originally saw this RailsCast a while back, but came back to it today for some info. I just want to say again that you are an amazing part of the Rails community.

Avatar

Great railscast, helped me get started with Prawn.

One Issue I had was using some of the Prawnto features such as bounding_box or any other item on:

http://www.cracklabs.com/prawnto/demos

I added the prawn.rb and folder to my lib directory then put your tutorial inside of:

pdf = Prawn::Document.new do

end

and was then able to use all of these features, you do have to put any variable declaration outside of this block though.

Here is some of my test code:

http://pastie.org/429045

Avatar

Great screencast !

If you are developing on a LAN with several machines you can easily extend what Ryan showed here to the LAN by just modifying the Name servers configuration.
In my system I just edit the database for Bind/Named, bump the version number and reload Bind/Named and voila - the app is avail to all machines on the LAN.
And please set up your DNS system such that you have internal and external databases - otherwise exposure could be a bit too broad.

Also note that you can limit the exposure of the apps by using the Allow From directive like:
  Allow from 10.1.0.0/255.255.0.0

Has anyone seen a head to head comparison between a Mongrel Cluster and using Phusion Passenger for deploying one or more apps.

Avatar

How can I delete a file in the generator? I'd like to delete the public/index.html if it exists.

And how can I remove all the files which are created with the generator?

Avatar

How to redirect back to @commentable index action?

Avatar

Hey, I am trying to send email on specific times selected by the user. How can I schedule job of sending email like that. I did not get that. Pls help.

Avatar

Hello Ryan,

thx for your great work.

I try to follow your cast to use multiple model update in a single form.

But i have a problem and don't find any solution.

I have validation for the model let's say the primary. In your example in the project model.
When a fill up badly the form at the first time (i have the correct error message) and i correct it afterward, I have strange thing in the database.
Let second model (task in your example) has no reference to the first model. Project_id is NULL

I have look in more detail what 's happen. And when i have the validation error at the first time, the task object are created in the DB and at the second time, i have alreeady the object in the DB, thus this isn't new_mouton_attributes but existing_mouton_attributes that is used!

Can you help me, i can send you my code if you want

thank in advance

Raphael

Avatar

Once again, your screencasts answer my question perfectly, going one-step further than most other tutorials on the web. The advice on where how to load the correct polymorphic model (find_commentable) is exactly what I needed to get my code working today.

Railscasts are undoubtedly the best rails tutorials on the web - keep up the great work!

Avatar

Regarding the redirect_to, there's no need to use the :id => nil hack. It can be more clearly done as so:

redirect_to [@commentable, :comments]

Avatar

I'm trying to understand why this approach is valuable. Couldn't the same practical result be affected by including belongs_to statements for all three models in the comments model and passing the type and id values to the index and create methods as parameters?

I'm not arguing against it; it is very cool. I'm just trying to understand the benefit gained from the additional complexity.

Thanks. These screencasts have moved me several steps ahead of where I was.

Avatar

Excellent screencast!

I need some help to create a comment destroy action. How to redirect back to commentable/[:id]/comments after deletion?

Avatar

I really liked $1.classify.constantize.find(value) trick. Superb cast, as always :)

Avatar

Thanks for the great post Ryan.
 
You forgot to mention routes helper polymorphic_url([@commentable, comment]).

You have very nice screencasts, please keep up the good work.
- Girish

Avatar

hey. I'm having problems with the path for images.
the pdf writer cant find it.
I've put the image everywhere but it doesn't seem to find it.

without restful routing. what should the path be anyway?

Avatar

For those who get the above error,
the author of prawnto extends the Memoizable class of ActiveSupport 2.2 to memoize a couple of methods.
Commenting out the following lines from /vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
should get things rolling..
-extend ActiveSupport::Memoizable
-memoize :ie_request?
-memoize :ssl_request?

Avatar

Hi Ryan,

Nice screencast :).
Im getting the same error as Richard above.. i too have ActiveSupport 2.1.0.
Should I be using an older version of prawnto or prawn?
I do not want to upgrade Rails.

Avatar

Hi Ryan, I've got a question for you: is it possible to have polymorphic nested associations, so that, for example, you could create comments on each parent model's creation page?

Thanks, any help would be appreciated,
Paul Gatt.

Avatar

Ryan, great screen cast as usual. One puzzling error I'm getting in running the Episode 154 code that is confusing is on trying to submit a comment to an article, on line 17 of the Comments Controller I'm getting a NoMethodError in CommentsController#create (The error occurred while evaluating nil.comments).

I don't know if it my setup, Ruby 1.8.7 with Rails 2.3.2, but it is certainly strange.

As always thanks for all the work you put into Railscasts.

Avatar

About Namespaces:

I've had that worry, too. I haven't tried this yet, but I'm thinking that I will put my code one folder deeper. In other words, if the name is my_engine, then my folder structure would look like this:

app
    controllers
        my_engine
            first_controller.rb
    helpers
        my_engine
            first_helper.rb

etc....

Avatar

How do I get updates of my Engine to my users?
In other words, if I add a feature to my Engine, is there any mechanism for getting the new code to the apps that are using that Engine, or do I have to copy the code by hand to each app that is using it?

Avatar

After installing prawn and prawnto I now get this error when I try and run the server.
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:275:in `load_missing_constant': uninitialized constant ActiveSupport::Memoizable (NameError)

Does anybody have any clues for a newbie.

Thanks

Richard

Avatar

might be useful to note that (at least with Rails 2.3.2), you need to setup your Workling dispatcher in an after_initialize block if you're defining it in each specific environment file.

see: http://gist.github.com/84308

Avatar

I'd just like some suggestions on how to do functional or integration tests when starling/workling is present!

Avatar

@Aditya, thanks. I do have a cold. It's partly allergies too.

@sthapit, STI wouldn't work well here because all three models (photo, event, article) have very different content. You wouldn't want them to share the same table.

@shm, thanks for posting that. I was debating which approach to go with while preparing this episode. Both ways should work well.

@Marc-Antoine, try something along these lines (completely untested):
http://pastie.org/425523

@amrnt, it will only become a problem if you're trying to pass both *_id fields as parameters in the routes. If it gets that complex then you may want to go with shm's solution mentioned in comment 7 above.

@wil, yes. My twitter name is rbates.

Avatar

Like Eric, my big question has to do with namespace collisions. I'm running some tests on that to see where it stands.

Avatar

Is there a simple way to move up or down by a single migration? I need to go through about 50 migrations, in order, examining the db after each (and looking at the migration output/errors.)

Moving along by typing out each version number is a pain. It would be great if I could do something like:

rake db:migrate VERSION=NEXT

Thanks...

Avatar

what if, I use in the same table 2 fields with *_id ?

Avatar

I'm trying to implement something like this with this example:

articles = User.articles_with_a_comment_from_me

I'm having trouble to figure how to setup the polymorphic through association in the User Model. Can someone help me?

Avatar

hi ryan,
very nice solution!

another approach i used in one of my projects is to define the 'commentable'-type (or "context_type") directly in the routes, as mentioned here http://blog.opensteam.net/past/2008/11/26/polymorphic_controller_nested_routes_polymorphic/

then you don't have to 'trust' the params-hash to return the correct "commentable" :)

greetings,
shm

Avatar

What you could also do is to have Article, Photo, and Event be STI subclasses and have their superclass has_many :comments. Both approaches work of course but newbies might want to also consider STI - it keeps the code cleaner for counter_cache, adding more common functionality (e.g. voting, page views counting etc.). I say this because we have a similar setup at socialect.com and debated for a long time before settling on STI instead of polymorphic associations. In our case Classes, Events, Places, Groups, Topics are subclasses and each of them has many votes, comments, pageviews, memberships etc so it was very easy to add that to just the superclass.

Avatar

Thanks again, Ryan. I just read Obie's take on this subject in The Rails Way a few days ago (I skipped through many chapters in that book!) This will become even more relevant (and timely) once people start realizing the potential for schema-less databases.

Awesome. :-)

Avatar

Cool, that find_by_commentable method is a pretty smart piece of code.

Avatar

Oh I figured it out now.

*SPHINK*
by *_Julius Francisco_*
*INSTALL* (_Leopard with Macports_)

Install sphinx via macports
<pre>
sudo port install sphinx
</pre>

Install thinkingsphinx
<pre>
script/plugin install git://github.com/freelancing-god/thinking-sphinx.git
</pre>

*Configurations*
Create a configuration file in _config/sphinx.yml_ with the following content. Port number varies on what you like, just make sure you will have different range if you are planing to use multiple instance of _searchd_(sphinx deamon)
<pre>
development:
port: 13000
test:
port: 13001
staging:
port: 13002
production:
port: 13003
</pre>

*Indexing*
Before _indexing_ you need to create a configuration file with each environment, or the environment that you are using, to do so...

<pre>
rake ts:config
</pre>

This will generate the _config/<env>.spinx.conf_

Before creating new index, or even reindexing you must delete the following files:

_indexed files_
<pre>
rm -rf _db/sphinx/<env>/*_
</pre>

_pid and logs_
<pre>
rm -rf _log/searchd.*_
</pre>

Now, time to do index:
_indexing and re-index_
<pre>
rake ts:index
</pre>

Starting sphinx:
<pre>
rake ts:start
</pre>

Stopping sphinx:
<pre>
rm -rf _log/searchd.<env>.pid_
rake ts:stop
</pre>

*_NOTES_*
Just make sure that your _port_ numbers differ from one application to another if you plan to run _sphinx/searchd_ in a single machine with the same database or even different database. _sphinx/searchd_ won't start if you already have a daemon running of the same port. Check your _log/searchd.log_ to verify that sphinx is running smoothly.