RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thank you for the great railscasts.

Avatar

Can some one help me with the f.hidden_field :product_id, :value => @product.id

comment controller
def create
    @Comments = Comment.create!(params[:comment])
    flash[:notice] = "Just Add !!"
    redirect_to product_path(@comments.product_id)
  end

error:
NoMethodError in CommentController#create
undefined method `product_id' for nil:NilClass

I keep getting this error..
does anyone know why this occur

Avatar

Hmm.. personally, I dislike using a bitmask due to the issues raised above.

I've just released a simple gem for roles that simply stores roles as serialized YAML:
http://github.com/mkdynamic/miracle_roles

Avatar

Love your screencasts, learn something new everytime! Thanks

Avatar

@graham:
For 'validates_attachment_content_type', you can add :allow_nil => true. For 'validates_attachment_size', you can use :allow_nil => true after a quick fix as follows:

Open your paperclip.rb plugin file.
Find 'def validates_attachment_size name' string.
Replace 'validates_inclusion_of' rows with the following:

validates_inclusion_of :"#{name}_file_size", options.merge(:in => range, :message => message)

Save it. Restart your server. Try again. Let me know if this works. Good luck.

Avatar

@elioncho

I had the same problem. I finally decided to monkey patch some of the nested attributes functionality to add an option :associate_existing to accepts_nested_attributes_for which takes a proc, the result of which is an instance of the associated model. This works pretty well, but it isn't tested. Good luck. Just plop this file into config/initializers

http://gist.github.com/282223

Avatar

I'm implementing the Remove checkbox option. When the user checks the first of the nested records for removal and I perform a page.reload, the line is removed; however, the first checkbox of the remaining lines is checked. This only occurs in Firefox. It's fine in Safari and Chrome. Does anyone have any ideas on how to fix this problem?

Avatar

Hey, just thought i'd say i had a problem running the 'hello world' bit, i was just getting a '#' on the screen. Sorted it by putting the 'Hello world' in square brackets.

def call(env)
  [200, {"Content-Type" => "text/html"}, ["Hello world"]]
end

Avatar

Very interesting stuff Ryan,

What are your thoughts on using this with a has_and_belongs_to_many relationship? It appears that it aught to work, yet there is no documentation for it. Seems rails doesn't support using this in that way?

Avatar

I have done it with Haml, Formtastic and jQuery too:

http://blog.js.hu/2009/06/15/add-form-fragments/

Maybe it's a bit old and doesn't have the latest changes but it's still working.

Avatar

Here's the Evil Hack that temporarily makes your counter cache attribute non-readonly so that you can initialize it properly during up migration - http://blog.bitfluent.com/post/30194408/updating-counter-cache-in-migrations

Avatar

What is the best way to, on error submitting a comment, render the original article/photo/etc page and show the error?

What would the render line be?

Aka without polymorphism you can do:

   render 'article/show'

but now you would have to have a case for each class type... or build the location via the class name...

Avatar

@David Souza - There are a couple of ways to think about that issue - some people may not expect any changes until they hit submit, as that's how web pages normally work, at least up until recently.

I like the idea of instant changes too, but perhaps more use feedback should be included, like flashing a message saying it has been deleted, when doing a ajax request...

Avatar

really nice screencast arrives just perfectly for one project

but I'm getting a strange error with an apparently non existent association in what would be the same level as :answers, is there any specific rails version involved ? I'm using 2.3.5

Avatar

Thanks, DanS! I love it! I never even knew to try that! (Re: TextMate's column edit)

Avatar

Whew, figured it out. Lets just say the paypal sandbox has become a good deal more onnerous since this railscast was first posted. Be sure and create a test seller account and a test website payments pro account from the same screen. Then log in to your seller account, click on profile to get the API sig, then make sure you agree to the wpp billing agreement. Otherwise your transactions will fail. Oh, and ignore every instance where paypal says it will set up api creds/bak accounts/etc for you automatically. It never works.

Avatar

n my controller. The method will return nil. Any way to get around this?

Avatar

Great tip! Was looking for a way to expressing a one_to_many relation in fixtures, and I found it! The new fixture in Rails 2.0 is fantastic superior.

Avatar
Antonios Chrysakis on #136 jQuery 2010-01-19 01:55:01

@ Brad S

Thank you! Thank you! After 12 hours trying to render the js files without the html the only think worked was your suggestion to add

exempt_from_layout /\.js\.erb$/

to the application controller!

Thanks a lot

Avatar

@carl

See the formtastic rdoc, there's an example for nested forms ;-)

Avatar

Fantastic screencast!

Anyone notice a bug with polymorphic nested tables?

1. create nested attributes on polymorphic model.
2. Add new item, with new sub item (polymorphic) but cause an error on the sub item (eg leave a validated field blank/wrong).
3. Returns to page with errors, anytime trying to submit this fails because of the missing polymorphic values being set.

At the moment, not sure how to fix it, but only enabling the nested model forms on edit screens of the parent item.

Thanks for the screencast!

Avatar

Hey guys. Hoping someone might still be reading these comments. I am not having any luck getting through to paypal and its driving me nuts. Everything works perfectly if I use the railscasts paypal_options from github (username, password, api sig). But if I use my own, I get "This transaction cannot be processed due to an invalid merchant configuration."

I have no idea what's broken. I have a valid sandbox account with a test seller account, which has a bank account and API credentials. Anyone?

Avatar

Excellent! Like Kikan I was stuck on the escaping problem.. h() never occurred to me as a solution. Thanks once again for a great screencast! :D

Avatar

nitpick, Ryan. The size of the movie files is swapped. Af tirst I'm getting curious, thinking "the iPod file is bigger than the .MOV ?", and proved otherwise. :) Downloading now. Thank you for good screencast, as always.

Avatar

Is this Rails3 compatible?

Avatar

You can find the old files Ryan uses here: http://github.com/ryanb/railscasts-episodes/tree/master/episode-170/blog/vendor/plugins/open_id_authentication/

Avatar

Maybe not. Looking at the migrations and the schema, there's a lot more going on than just the tests I see in the missing rakefile.

Avatar

You can find the "missing" files here: http://github.com/rails/open_id_authentication/commit/16e40d2ee73f8f10c2cb77234b6f5f4816c94547#diff-0

It appears they are just a bunch of tests, so you can skip "rake open_id_authentication:db:create"

Avatar

The rakefile and model generators are no longer part of the plugin. They were removed several versions ago.

Avatar

Hi, Ryan.

Correct if I'm wrong, but, shouldn't the code in the ApplicationHelper be on this page?

Thanks.

Avatar

What if I need to have a 'observe field' in the child class?

For instance, we have a named list with many books (one list has many books) and when one types the ISBN to add it to the list (the number of existent books is too big for a selection field) the system must display the name and price of the book.

Avatar

is there going to be screencast on nested forms that covers validation?

Avatar

class Message < ActiveRecord::Base
  belongs_to :author
  accepts_nested_attributes_for :author
end

I'm using the above and it's working perfectly. The message gets saved with an author_id attribute which references the author (tha author record gets saved too).

The problem:

Sometimes I don't want to save the author because the author already exists (I just want to reference it). So, I just want the message to get saved with the author_id reference. How can I override or modify the normal saving behaviour when using the accepts_nested_attributes_for method?

Avatar

What if I need in partial views/surveys/_answer_fields.html.erb access to some answer attribute, for example answer.id, just use there f.object.id ? Not very nice solution, anyone has better one ?

Avatar

Ryan, Thank you. I didn't have the mocha gem installed. Got a lot of "NoMethodError: undefined method `expects'/`stubs' for ...". But once I installed it, all the tests passed. - Mike
 my web page <a href="http://www.jwebtasarim.com" >web tasarım</a>

Avatar

One thing I would say about this is that it is probably better and more intuitive keeping the "remove" part of things separate from the concept of nested forms, don't use "_destroy" fields.

Associations can always be destroyed with standard destroy paths using the Restful API. If you want spiffy dynamic forms, then use AJAX calls to actually destroy the model when you click the link rather than just hiding it and hoping the user knows to update the form.

Another big plus is that the system would still work without javascript which is not true in this case.

Just my two cents.

Avatar

Thanks for this one, Ryan!

A third part would be nice, like in complex forms 3, where you add/remove checkboxes dynamically ;-)

Avatar

Nice and useful cast as always. Tnx Ryan.

For those of you that are searching for unobtrusive jQuery version of this functionality, take a look at last answer at http://stackoverflow.com/questions/1704142/unobtrusive-dynamic-form-fields-in-rails-with-jquery.

Hope this helps

Avatar

Hi Ryan,

Looking forward to the nested_form plugin. We already love the Formtastic forms you introduced a few weeks back. I hope we can continue to use that when we try out the nested_form plugin.

Cheers,
Aditya

Avatar

Nice !

I searched for two months why I couldn't nest an add field in another without having a double javascript escaping, resulting to a messy display in the view.

The "simple" h() you use is the solution and I could get rid of my bug now !

An I like the use of "class" and the separation of Javascript / Ruby, and your convention of naming fields with _fields. It's very clean : I will cleanup my code doing this now.

Thanks a lot.

Avatar

@kit that's the problem when using gem in environment.rb. i also wondered about this.

install the plugin and gem by hand like you see in the video and they appear.

a little bit ugly since you enter it in environment.rb and install it with rake gems:install, but you still have to set up them manually.

Avatar

i'm confused by your routes. you say that builds upon screencast 160. but in 160, you haven't used "session" but "user_session". is this the same?

Avatar

Josh, that's a great question. I've had the exact same one in scouring the web for rails ecom solutions. Right now I have a single cart per user, and multiple orders which are created based on the cart's contents. Once an order is complete, the cart is cleared.

However, now that I'm actually integrating the paypal code, this isn't going to work, as a unique "invoice id" is required for every express checkout. Which leaves me wondering if I should be creating a new cart every time, and making that the order, or as you say, starting with orders which seems to be a cleaner solution rather than having bizarre order <-> cart relationships.

Avatar

Ryan - you are my savior -

needed a solution like this for some time now. But i still have a question

How can i build a survey with many questions and many answers but all user see the same questions and can have only one answer per question. Only the admin can see and edit multiple answers and questions. Your help is very much appreciated

Avatar

It looks like rails/open_id_authentication no longer provides a rakefile or the model generators. The repo in your video had 38 files, but the current version only has 11.

Any thoughts on where these have gone?

Avatar

hrm, a bit stuck here, the rake task open_id_authentication:db:create doesn't appear to exist.

I have the authlogic, authlogic-oid and ruby-openid gems installed.

If anyone comes across the error "llformed requirement ["=>0.2.1"]", there's a typo in the latest version of open_id_authentication.
Change

    :version => '=>0.2.1'
to
    :version => '>=0.2.1'

Avatar

Sorry I can't get to everyone's question, but for those having trouble with the "a[:content.blank?" and "_destroy" behavior working, make sure you're using the latest version of Rails (2.3.5). This behavior has changed recently, and even some of the documentation is out of date.

Avatar

Ryan, thanks for the video on this.

But how about addressing this with has many through? Right now, I am trying to make an account sign up form. My app has an Account which has_one company, Company has many users, and Account has many users through Company. I want this form to have fields for the Account, the Company, and the User. Rails is giving me all sort of problems trying to do this http://pastie.org/780854 . After seeing tons of people with the same question in forums with no answers, and not too much help in IRC, I think this would be a great talking point.

Thanks, let me know