RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

@Sergei

Look into polymorphic Paperclip attachments :)!

Avatar

Does anyone know how can we use this method with Paperclip plugin?
I mean, when we have Survey model and Photo model for example.
Every Survey has many Photos, i.e. Survey has multiple attachments.

Thanks in advance!

Avatar

Love your casts Ryan. Any chance of getting one for integrating NEO4J with RoR?

Avatar

As always, thanks for the great cast.

I was wondering if there was a way to access the underlying model from within a form builder, to use data from the db to dynamically create things like the "labels" for the form.

So let's say you have the following form: http://pastie.org/780413

Is there some way that I can get data from the db for instance for the label. So that in stead of "Question", I can do something like question(:name) or question.name or question[name] or whatever it would be. I've tried a lot of different things.

I'd be using it in a different context, so that would explain why it may not make much sense in the current example.

I'd appreciate any help.

-A.

Avatar

Just a gotcha.. Windows/ImageMagick doesn't seem to like single quotes in the argument in the reprocessing.

" -crop '#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}'"

I changed the single quotes ' to \" (escaped double quote), and it worked right away.

Avatar

I also don't understand why we had to define user here: (:friend_id => user)

Avatar

What if you don't use the plugin? What will the value of current_user be?

Avatar

To anyone that's having problems with this tutorial:

THIS IS OLD. Authlogic has changed and this tutorial will no longer get you where you need to be.

Go here instead:
http://github.com/binarylogic/authlogic_example

Avatar

Hello, thanks for the cast :)
Unfortunately i get the following error:

can't dump anonymous class Class
i have the following code:
def datei_speichern(pfad)
   name = File.basename(pfad)
   teile = pfad.split("?")
   puts teile.size
   puts pfad
   if teile.size == 2
   puts "PHP-Skript"
   else
   if db_vergleich_neu(name)
datei = @agent.get(pfad)
datei.save_as('l://trebble//teufel//' + @titel + "/" + name)
end
end
rescue
puts "fehler"
end
handle_asynchronously :datei_speichern

Any Idea whats wrong?

Avatar

Thanks a lot. It was a great collection

Avatar

Is it possible to do result highlighting with Sphinx to show the search terms?

Avatar

Ryan, thanks a bunch for this. It's been such a great help. One question: I am running into trouble when my associated entity already has an index.html action. Can you please tell me how to inform the controller of how to discriminate between index.html and index.js?

thank you!

Avatar

I managed to get the counter caches to decrement. It's ugly and probably excessive. But it does work.

def assign_tags
 if @tag_names
  new_tags = @tag_names.split(/\s+/).map do |name|
   Tag.find_or_create_by_name(name)
  end
  # destroy taggings to decrement counter cache
  self.taggings.each do |t|
    t.destroy if !new_tags.collect(&:id).include?(t.tag_id)
  end
  # assign any new tags
  self.tags = new_tags
 end
end

Avatar

Looking forward to Parts 2 and 3!

Avatar

I tried # OR part in the controller and now i get "wrong number of arguments (1 for 2)"

I dont get what i keep doing wrong

Avatar

I get a runtime error when i try to access the search form page. I can search fine in the script/console but when i got to my search page i get " Showing app/views/groups/search.html.erb where line #1 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"

I cant figure out what is causing this problem

Avatar

I didn't read all of the comments but I just ran into something on rails 2.3.2, when I use the

:reject_if => lambda{|a| a[:content].blank? }

it was rejecting everything, I needed to change it to

:reject_if => lambda{|a| a['content'].blank? }

Not sure if it was something silly I was doing or not but worth mentioning if it saves someone some time.

Avatar

gem install ruby-debug19

for ruby 1.9 compatibility :
http://isitruby19.com/ruby-debug

Avatar

For those looking for a full IDE I believe you can't, by a long way, do better than RubyMine 2.0. Features are added super fast. Within a few months of becoming aware of HAML and Cucumber the team had integrated and shipped support.

Check it out at http://www.jetbrains.com - it is commercial but a license is not that far off the price of TextMate.

Avatar

At what point is the anonymous scope evaluated? put another way, what is the trigger that says "I have finished chaining scopes, now run the query" thanks

Avatar

Great screen cast, just what I need right now!
I'm trying to understand the :reject_if => lambda { |a| a[:content].blank? }
I have forms with multiple inputs and checking for blank inputs wants all of them filled out. For example, if instead of "Question" as the input it had "First Name", "Middle Name" and "Last Name". How can you check that the first name is not blank but have it be optional whether to fill out the middle name? Which levels of the nesting attributes have to have no blanks to validate?

Thanks!

Avatar

Another great screencast and glad you're updating your Complex Forms series. That's been one of my favorites, but

@ryan, or anyone for that matter, do you think you'll ever address how to get the same model multiple times in the one form?

Avatar

Anyone have experience with conditional validations in paperclip?

In my case, an image is optional, but if it's present, I want to validate its size and format.

Any ideas or code?

Avatar

@steve I had the same issue. I think Ryan's setup must have magic in it cause the api says to do it the way you did and not with :symbol.

Avatar

I had the hardest time figuring out why I couldn't get any of the Javascript to run. I finally got it to work by restarting the server after issuing the

script/plugin install git://github.com/rails/auto_complete.git

Avatar

Nice work Ryan - Thanks for picking up complex forms again.

Btw. is there a simple way to associate already created questions with a new survey?
Can a survey contain questions that are already part of other surveys?

Thanks
yanosz

Avatar

David, Ryan is using Textmate's column edit to quickly change the "builder." to "f." To use this push and hold the option button, your Textmate cursor changes to a cross, left click and drag the cross to select the column of text you want to change. Once you have a selection just type the new text you want.

Avatar

StuFF mc, Ryan is using the Textmate "Ruby on Rails" bundle to create the partials. This is available with "control shift H" or from the bundle menu "Ruby on Rails > View Templates > Create partial from selection"

Avatar

_delete vs _destroy:

DEPRECATION WARNING: _delete is deprecated in nested attributes. Use _destroy instead.. (called from _delete at /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/nested_attributes.rb:263)

Thanks!

Avatar

Ryan, I'm with Lyle. Is there any way you can get this on the opposite end of the relationship on the belongs_to? It should work is what I've seen on the internet but I keep getting errors.

Also, does it work with anything other than a has_many with belongs_to relationship?

Avatar

I asked the same question Yinghai, someone claimed you could, but I've not yet see it work.

Avatar

Is it possible to use mechanize to fetch images and such from a web site or is there a better gem/plugin suited for this?

Avatar

Thank you very much for your work.
Is there a way to integrate accepts_nested_attributes_for and formtastic?

looing forward to see your part two

Avatar

@Matthew: I have encountered the same problem. Running the console everything's fine but when trying to open the site in a browser passenger obviously tries to load ActiveRecord which then complains since no database is configured. After hours of trial and error I will give up on MongoMapper until it works fine.

Avatar

Ryan, when you work on the second part of this tutorial, add a belongs_to relationship to Survey. For example: author. In survey.rb add accepts_nested_attributes_for :author. Incorporate this into your form.

I mention this because nested forms doesn't handle belongs_to relationships properly. If you want to change the author for a single survey, it will do something like:

update authors set name = 'bar' where id = 1

Which affects all surveys that point to that author.

What it should do is:

insert into authors (id, name) values (2, 'bar')
update surveys set author_id = 2 where id = 5

Avatar

Great screencast, Ryan. I found myself using the Complex Form series quite a lot in my work. I am loving passing these as attributes so easily.

I did wonder about a TextMate-ism in your railscast. I noticed once you made your partial you were able to start at "builder." and select up to builder on multiple lines and then used a key combo to select only "builder" in order to change all instances to "f". Is that a custom item in a bundle or is it available and I just don't know about it?

I did notice some updates already with Rails 2.3.5.

1. _destroy has been changed to _delete
2. a[:content] is not recognizing the string representation of content, so it must be a['content']

I am loving the new changes and can't wait till Part 2!

Avatar

@Chris Lerum you need to do give the nested_attribute free:
attr_accessible :questions_attributes

Avatar

I got some problems using cancan.
Its working ok, but after ive installed the gems, this is the output of my rake gems:install:

sudo rake gems:install
(in /Users/tscolari/Projetos/skeleton)
rake aborted!
uninitialized constant ApplicationController::CanCan

(See full trace by running task with --trace)

Avatar

Ah, it seems to be an issue with passing in the content as a symbol. If I do it like this it works fine:

:reject_if => lambda { |a| a['content'].blank? }

Can anyone help me understand why?

Avatar

For some reason, I can't get this to work:

:reject_if => lambda { |a| a[:content].blank? }

It simply won't save the questions to the database. Anyone else having that issue?

If I remove it, the questions are saved but blank ones are saved as well - just as in the screencast prior to adding that line to Ryan's controller.

Avatar

Great cast!!! and thank you very much Andrew Cetinick for your comment on validation, works perfectly!

Avatar

Thanks Ryan! Great tutorial, as I was struggling in a project with many nested models.

Avatar

really nice tutorial, looking forward for part two. Also interested in deleting and sorting elements using js

Avatar

hi there,
 
 Like Ritesh who posted on Dec 16, 2009 at 08:22 ,
I am also facing a problem with the undefined method "order".

 Please help, Ryan?

Avatar

I guess it's not _destroy but _delete. The _destroy was not working for me and I looked at railsbrain.com and it was actually _delete.

http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002182&name=accepts_nested_attributes_for

Avatar

Good post.It is very usefu.I like to see it because I am learn it now.Thank you.

Avatar

You are amazing Ryan! I was just bashing my head in the other day wondering how to pull this off with my controllers spazzing out at one another. Not the best task for a newb.

It's trippy how you always seem to find what I need at the right time.

Thanks

Avatar

@Brad. I have the same problem with decrementing the counters. I even tried adding before_destroy and after_destroy to the taggings, but they don't even get called. I guess that they are deleted and not destroyed. Thoughts Ryan?

Avatar

Thank you Ryan for another series of screencasts!

Hopefully this time you will cover long-awaited validation in deeply nested forms.

Thanks a MILLION,
Boris

Avatar

Will you do a railscast about the plugin you use for the snapping of the fingers? :)

P.S. This cast is my favourite! Can't wait until next week.