RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thomas, that fix worked great. Thanks!

Avatar

how to use this functionality to import data for model associations

This is my case

class SampleRequest < ActiveRecord::Base
has_one :sample_request_text_excerpt
accepts_nested_attributes_for :sample_request_text_excerpt
end
class SampleRequestTextExcerpt < ActiveRecord::Base
belongs_to :sample_request
end

In the sample_request model, asset type field would be text and image , for text type different excel sheet and for image type different excel sheet , only certain field would be extra in the sample_request_text_excerpt model.

For single model no issues , while try to use for associations

this line showing error
sample_request.attributes = row.to_hash

while uploading the text spreadsheet error displayed

Avatar

Fantastic episode, has helped me a lot. Can I suggest a tweak to the remove_fields coffee script :

$(document).on 'click', 'form .remove_fields', (event) ->
$('input[name$="[_destroy]"]', $(this).siblings()).val('1')
$(this).closest('fieldset').hide()
event.preventDefault()

This makes it less dependent on the position of the hidden destroy field.

Avatar

Hi Ryan,

Can you please update the episode as it is for Ruby 1.9.2 and rails3.0.1.

It is throwing error for "ActionController::RoutingError (undefined method `referenced_in' for Article:Class" when using rails 3.2.9 and ruby 1.9.3

Thanks,
Vardhan.

Avatar

Yes I have done enough poking around on forums to understand that I have something running, its just that its a fresh rebuild... I didn't think anything would be running because of that.

That being said... How do I close this running thing?

Thanks,

Sam.

Avatar

Hello I learned a lot from this episode about Polymorphic association, which always seemed a bit mysterious!

I think what the episode is really missing, is an explanation about polymorphic_url or polymorphic_path in order to recognize which model you wish to redirect. It is extremely helpful to understand this method especially when nesting multiple polymorphic models (see documentation). For example

ruby
def update
     @comment = Comment.find(params[:id])
     @comment.update_attributes(params[:comment])
     response_to do |format|
         format.html do
               if @comment.errors.present?
                     render :edit
               else
                     redirect_to polymorphic_path(@commentable), :notice => "Yay updated!"
                     # or
                     # redirect_to polymorphic_path([@commentable, @comment]), :notice => "Yay i can update and see further down the path!"
               end
         end
     end
end

sorry if messed up the comment markup ^^

Thank you Ryan for the wonderful videos. I have been watching your masterpieces for a few years. I love your Pro and Revised versions, Keep it up!

Avatar

@Gerald: Thanks so much for this fix. This totally solved my problems :)

Avatar

Hello!!!,
I'm not able to use autocomplete, I'm getting the following issue;

ReferenceError: Ajax is not defined

TKS :)

Avatar

i have <%= button_to "游客入口", signup_path, :method=> :post %> and
def create

@user=params[:user] ? User.new(params[:user]) : User.new_guest
if @user.save
  flash[:success] ="注册成功"
  session[:user_id] = @user.id
  @user.type=workers
  redirect_to '/superadmins/inspect'
else
  render 'new'
end

end
but when i click button it can't creat a new guest.it just link to signup_path.Doesn't the method post work?

Avatar

It means you have another web server in your vps, close it.

Avatar

Thanks for yet another great set of videos. Quick question however. Let's say your article would have a 1-n association with a tree-like structure called 'article_subjects', and that you would also want to be able to search based on these article_subjects (and their parents). Any idea on how we could achieve this?

Avatar

Could anyone let me know why I am receiving this error on postback?

Could not authenticate you from Twitter because "Invalid credentials".

Avatar

Ryan, what do you think of audited gem - https://github.com/collectiveidea/audited. This seems to be another solution to provide similar functional.

Avatar

Ok I understand. I'm not sure I'd want to back out of the transaction because the activity tracking failed though... the recipe saved, the notification failed - seems like a secondary transaction to me, failure of which would be logged, retried and possibly even pushed onto a queue or other out-of-band process.

Avatar

I take back what I said. It is more of a time saver than I had originally thought. I've posted the below generator.

https://github.com/kobaltz/app_template_generator

It creates
- Twitter Bootstrap
- Sorcery :remember_me and :reset_password
- CanCan 2.0
- Simple_Form

Ran a few successful tests. Definitely cuts out the BS in the setting up of base app and gets into the fun part. Almost reminds me of the yii demo app or asp.net mvc template app.

Huge time saver and less running around to find the old scripts for the authentication reset password stuff!

Avatar

I'm betting you're trying to deploy to an instance with 8GB of storage (like a T1.micro)? Try adding the following to config/rubber/role/mongodb/mongodb.conf:

  smallfiles = true
Avatar

Check out Ryan's nested form gem wiki.
https://github.com/ryanb/nested_form/wiki/Integrating-with-client_side_validations
Hope this could help you. I tested here and it was ok.

Avatar

As always. I'm looking at angularJS because I have to rewrite something.
Let's check out Railscasts... BÄM.. Tutorial for exactly that.

Mindreader.

Cheers

Avatar

Anyway to convert the sort_links into bootstrap buttons?

I managed to apply a form-inline class to the search_form_for thanks to Tom Harrisson, thanks!:

<%= search_form_for @search, :html => {:class => "form-inline"} do |f| %>

Any idea how to apply a css custom button to:

<%= sort_link @search, :title, "Title" %>

Thankyou

Avatar

You should always do both. Never rely on client only :)

Avatar

I have had the same issue with equality. I ended up letting Struct solve the equality issues for me. This preserves equality for usage with arrays, hash (keys), sets etc..

The example below decorates ServiceType with start_date & end_date from another model:

ruby
ServiceTypeWithDates = Struct.new(:service_type) do

  attr_accessor :thing_with_start_and_end_date

  def initialize(service_type, thing_with_start_and_end_date)
    super(service_type)
    self.thing_with_start_and_end_date = thing_with_start_and_end_date
  end

  delegate :start_date, :end_date, :to => :thing_with_start_and_end_date
  delegate *ServiceType.column_names, :to => :service_type

end
Avatar

Hi Ryan (and all),

I'm getting times near to 10ms when retrieving search suggestions (using redis). BTW, my dev enviroment is a Mac Book Pro (8GB Ram, intel core I7)

Any clues on why I'm getting such bad response times? Based on the video I guess they should be better.

Avatar

I just found a issue in version 0.2.0 where it only permits attributes that belong to your model any additional fields are ignored. If any body else is facing this issue you can downgrade the gem by doing

ruby
gem 'strong_parameters', '0.1.6'

and then running bundle

Avatar

Hey all very green here, first time using Linode

(early on in the tutorial)
When running:

root@li511-230:~# service nginx start

I get:

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

Any suggestions would be greatly appreciated. Thanks.

Avatar

@Er.Love the syntax is:
Model.where(name: /#{params[:q]}/)
I hope you found it since then ;)

I'm having an issue. the selected tokens are not sent when I validate my form. I all the other fields are send except those ones!
I'm running rails 3.2.9 with mongodb

Avatar

Just a small note in case others had the same issue i did:

This:
# specify what domain to use for mailer URLs
config.action_mailer.default_url_options - { host: "localhost:3000" }

Should actually be:
# specify what domain to use for mailer URLs
config.action_mailer.default_url_options = { host: "localhost:3000" }

Once i changed the - to an =, it worked fine. Thanks again for a great railscast and gem!

Avatar

friend_id comes from the sample "Recipe Application" which has "friends".

Avatar

Ryan, as always great video.
But, I am having trouble with credentials need to get in to a secure Soap api. Also, now there is a new version of Savon. I was wondering if you can do a revised tutorial which will include using an action that requires login, if possible.

Avatar

I'm having the same issue. Does someone knows the answer?

Avatar

+1

I'd love to see a cast on Redis taking activity feeds to the next level Ryan :)

Avatar

Hi!

Can you tell what performance issues you have?

Avatar

Fantastic clip there, thanks for sharing.

Avatar

Thx for this episode. Where is part 2? :)

I think this episode needs a part 2 or maybe part 3 episode since most rails apps are more complex. Would be great to have a proof of concept for a website with different pages, authentication, authorization.

Have a nice day.

Avatar

ruby-1.9.3-p362 is buggy, and it's a known issue. so reinstall newer stable version(like 1.9.3-p374)

Avatar

ruby-1.9.3-p362 is buggy, and it's a known issue. so reinstall newer stable version(like 1.9.3-p374)

Avatar

wow, looks good!
I'm using DataTables, but will be switching!

Avatar

I implemented an activity feed last year which was a bit more complex than this. It had to store data such as specific actions like updating a models state column (from/to), as well as displaying consecutive activity grouped by the person who did it, and grouping the same consecutive action by the same person as "XX updated XXX (4 times)". Would be nice to see another pro episode that handled this display related stuff, since my code is kind of messy, and it's be cool to see how it might be implemented by someone else.

Avatar

I need a litte help.

Where is the rigth place to define the server's address for a remote json ?

tks.

Avatar

I'm not sure what version of jquery-rails used to produce this error, but it looks like there's an issue with jquery-rails version 2.2.1 and jQuery File Upload 0.4.1. I downgraded to jquery-rails version 2.1.4 and that did the trick.

Avatar

It looks like in current rails (3.2.12 and sometime since this episode), application.rb now combines the require frameworks into rails/all. This make commenting out require 'rails/test_unit/railtie' problematic. So it runs the tests in default directories (the first test run: partial based on rails defaults).

Per this episode we are loading all the *_test.rb files which allows for the custom directories (the second test run: full).

There is probably a way to do the equivalent of commenting out the rails/test_unit/railtie require without having to be responsible to maintain the other requires that rails/all buys us... however you can get around this for now by adding in minitest.rake:

Rake::Task[ :test ].clear  #added
Rake::TestTask.new( :test => 'db:test:prepare' ) do |t|