RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I forgot this little monkey patch to get true the validation

Use file_field_tag instead of file_field

<%#= f.file_field :image, multiple: true, name: "painting[image]" %>
<%= file_field_tag(:image, multiple: true, name: "painting[image]") %>

Avatar

HI Ryan,

Well done on this one. I followed all your steps and it worked like a charm.

NOTE: I did have to restart the server after modifying the development.rb file. Any others who run into any weird snags along the way, just restart your server before trying anything else.

Avatar

SUCESS!!
jquey-file-upload is not fully compatible with jquery 1.9.1
adding the jquery-migrate-1.1.1 plugin did the trick

Avatar

Thanks for the fix! Can you explain why this worked or what changed to cause the error in the first place?

Avatar

Yeah, but thats exactly what I wanted to avoid doing. My model is this message board:

A User has FeedItems. Each feed item is polymorphically related to one other model object e.g. TextPost, Picture, File, SpreadSheet and many more, via FeedItem's postable polymorphic property

So the problem here is with FeedItem: When showing User's FeedItems, I want to do something like:

ruby
@feed_items = current_user.feed_items.includes(:postable)

But since postable is polymorphic, i cant and i get the n+1 problem...

Avatar

in the OrderTransaction class there is an exception rescue but if the exception is triggered by the gateway, there is no way you can catch that into the transaction, am I wrong?

Avatar

The way I approached eager loading with polymorphic associations is to define the relationship as non-polymorphic for the types of polymorphic you need to eager load. Only works for cases where your eager loading is of the same type.

Avatar

I am finding this to be the case as well. Have you found any work arounds?

Avatar

One other thing to do: replace all occurrences of <% form_for ... with <%= form_for ....

Avatar

I'm also having issues implementing this soulution with nested forms, has anyone found a solution to this.

I've posted for help on stack overflow http://stackoverflow.com/questions/15599613/multiple-image-uploads-in-a-nested-form-with-carrierwave

Avatar

Strange. I updated rails to 3.2.13 and it broke again, pulling it back to 3.2.8 and nothing could bring it alive again

Avatar

For those who are curious, the following are the types of events that can get triggered for fetch() if you decided NOT to do {reset:true}:

  • 'add' - this gets triggered for EACH NEW model (not already in collection)
  • 'change' - this gets triggered for EACH EXISTING model (already in the collection)
  • 'remove' - this gets triggered for EACH model thats no longer in the collection after the fetch()
Avatar

Hi,

If anyone is having trouble with the collection's 'reset' event not triggering after calling this.collection.fetch(), it's because as of v1.0, the fetch() function only triggers the 'reset' event if you add {reset: true} as an option.

So, that line in the Raffler.Router.Entries() initialize function should actually look like this:

coffee-script
initialize: ->
  @collection = new Raffler.Collections.Entries()
  @collection.fetch({reset: true})

This is from the backbone.js site (Linky):

" If you want to smartly update the contents of a Collection, adding new models, removing missing ones, and merging those already present, you now call set (previously named "update"), a similar operation to calling set on a Model. This is now the default when you call fetch on a collection. To get the old behavior, pass {reset: true}."

Hopefully, when you have some spare time (ha!), you could update this tutorial with that little tidbit =)

Thanks for all your tutorials, I have been learning a ton!

Avatar

+10 - the Wiki entries on testing are definitely lacking

Avatar

I downloaded the example and ran it without any luck, seems like something is broken.
Tried with Chrome and Safari.

When dropping files, nothing happens, when adding files from the dialog, nothing happens.
Can someone confirm or am I missing something.

Avatar

I follow everything exactly, but when I try to display the graph with the fake information, nothing displays. What could possibly be going wrong?

Avatar

I cant seem to find a good way to do access control on a published message. So i publish a message using https://github.com/railsware/smt_rails so rails and javascript can share a mustache file but i only want an edit or delete button on the message if you receive the publish and you are the creator. my question is how do you publish something that can use 1 template(mustache) that can do access control on both client and server side?

Avatar

+1 for eager loading with polymorphic associations. A very common and real problem that should be addressed in a Pro episode. Thanks.

Avatar

Great episode! But I was hoping you'd touch eager loading with polymorphic associations. I couldn't find good information about it, and I feel it's a common problem.

Avatar

It's already fixed. binding_of_caller works with Ruby 2.0.0p0

Avatar

Just wondering, being pretty old... is this railscast still viable or is there another tool we should be looking at?

thanks,
rich

Avatar

I immediately cringed when I saw you eschewing the built-in ActiveModel::Validations validators for a custom validate_properties method. Then when your solution to allowing a more complex field to be added was to hard code it I lost all faith.

I actually had to do something similar about a year ago for a project collecting electronic signatures for forms. To meet my needs I used a dynamically defined proxy object that would allow the use of any built in validators on any of the attributes, then passed that to the form builder. I'd love to see this thought through better, but it already more effectively re-uses components that have already been built and tested.

Avatar

With Ubuntu 12.10 you need to install software-properties-common because add-apt-repository is not in python-software-properties.

You get the following message (for our googlers):

bash
bash: add-apt-repository: command not found
Avatar

Hi,

I've the same problem.

I'm trying to deploy to an instance with 8GB of storage (T1.micro).

Adding 'smallfiles = true' to config/rubber/role/mongodb/mongodb.conf did not work for me.

Did someone solve this problem?

Avatar

Hey, 1st I love the Railscasts series. You're a gifted instructor. Thanks!!

2nd, for one of our apps we needed to use Oracle with active record. I assume I can use Carrier Wave the same way, as if it were just a simple rails with mysql set up w/o any difference. Is that correct? If it's different, can you comment on how it's different? Thanks.

Avatar

I have a question on redeployment. Say if I have updated my application? Must I redeploy the entire application or can I use a pull request?

Does "cap deploy" flush the db every time I redeploy?

Avatar

Redcrumbs looks good... I tried it on one of my app and while overall it works good, BUT It relies on callback methods.. So lets say I have a post (created by User B) which a moderator (User A) edits... The creator of that crumb would still be the original post owner(User B) and not the moderator(User A)...

How to make it reference the moderator as the creator of the crumb/change since he was the one to edit the post?

As Ryan rightly pointed out, using callbacks becomes messy...

Avatar

Thanks for this. It'd be great to see a RailsCast or two on Backbone Marionette.

Avatar

If it isn't obvious from my screen-name, disclaimer is that I work at opscode...

The latest community cookbooks maintained by opscode are here:

https://github.com/opscode-cookbooks/

Since there are 137+ repos in there, you can sometimes find specific cookbooks on github which may be more up to date or may better fit your needs, but that's a good place to start looking. Since those cookbooks are designed to be multi-operating system (CentOS, Ubuntu, Solaris, etc) they also aren't necessarily the simplest designs.

Installing chef has also gotten considerably easier, and this:

curl -L https://www.opscode.com/chef/install.sh | sudo bash

Will install chef-client and chef-solo into /opt/chef and setup symlinks into /usr/bin/chef-client and works across most major operating systems. You can still gem install chef-client, but the omnibus installation into /opt/chef gets us a consistent (and usually substantially more up-to-date) version of ruby and rubygems across platforms including Solaris 9 and CentOS 5 which we support.

The site-cookbooks method of separating your own cookbooks has also been deprecated in favor of using berkshelf:

http://berkshelf.com/

There's good integration between berkshelf and vagrant for easily testing chef-solo recipes:

http://www.slideshare.net/bluepojo/introduction-to-berkshelfvagrant

The chef community has changed fairly fast in the past 12 months...

Avatar

My setup is sending blank emails too. Did you ever figure it out?

Avatar

I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.

Avatar

For an introduction to state machines, I would highly recommend this series of lectures: http://www.youtube.com/watch?v=HyUK5RAJg1c

It's fun to watch, and he goes through enough examples to make the concept very familiar by the time you get through it.

Avatar

Is it just me or is having a topic_attributes method on every class a terrible thing to do? It means you will have references to other classes, particularly global things like current_user, which should never be accessed inside your model, inside every single model you have.

I can see the code quickly devolving into spaghetti with references to relations and methods of current_user. It's adding inter-model dependencies for no reason, and moving a lot of the logic back into the model that strong parameters was designed specifically to avoid.

Avatar

Off topic but how do you get SQL syntax highlighting when using tail?

Avatar

Using delegate :name, :to => :category, :prefix => 'category' would simply do category.name rather than read the loaded/cached category_name attribute. AFAIK

Avatar

This is great, i love that it isn't a gem so I'm more likely to read the code & learn.

I'd like to add to this to allow for events that occur over multiple days. Does anybody have any advice on how to go about this?

Avatar

good episode. But what about delegate? Where is the differnence to your:

ruby
def category_name
  read_attribute("category_name") || category.name
end

Thank you

Avatar

A couple of things I found:

  • The subtree.arrange scope does not work if you have a default scope on your model, you also cannot use the inline unscoped method to undo the default scope, as this also undoes the 'subtree' scope. You have to use
ruby
Model.unscoped do
  @model.subtree.arrange
end
  • The recursive function given in the cast does not correctly handle the situation where you have siblings in the subtree. In this case it incorrectly emits empty <div> sections.

  • You must add attr_accessible :parent_id or some other accommodation so that adding new sub-messages does not produce a mass-assignment exception.

Avatar

Ryan is awesome but the complexity of the solution is huge and I hate it.

Avatar

I, too, would like to see this. Otherwise, great episode, as always!

Avatar

If you're using Cancan, you would stick 'skip_authorization_check :only => [:show]' in the appropriate controller.

Avatar

If I would opt by using an external table (properties -> name, value); how it would looks like?

I think the models could be something like this.

class Product
  has_many :proprieties 
#...
end

class Property
  belongs_to :product
end

But, what are the best way to bind this to the view?

Avatar

Perfect, I was using Simple Form then I had to use a different css path for that.