RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Why is this better than "sudo gem install rails –source http://gems.rubyonrails.com?"

An obviously non-programmer newbie.

Avatar

@Russell, I added a link in the show notes, hopefully that will be okay.

@Trevor, you're right, good point!

Avatar

Note that you can use the depth option in git to keep your "frozen" vendored much smaller by not downloading all of the project history:

git clone git://github.com/rails/rails.git --depth 1

Avatar

Hi,

thanks, this is really a great screencast.
I can use the :collection and :member but still dont know the exact difference between them ? Could anybody tell me ?

Thanks !

Avatar

Awesome episode Ryan, thanks. One note: I couldn't find the link to your contest results anywhere on your homepage, though i was able to derive it from the screenshot at the tail end of this episode.

Avatar

(ò_ô) What!!

@Mikel, congrats!

Avatar

Ryan, thanks for the great episode on GIT! I was hoping you would do one like this and I wasn't disappointed.

Also, thanks to all the sponsors for the grand prize, I really didn't expect to win, but it is a lot of fun helping others and I am glad I could help and help other people learn more about rails!

Mikel

Avatar

When I try to download the "exception_logger Plugin [ryanb fork]" from GitHub - the archive doesn't have all the files.

Avatar

Hi Brian...like everyone else, those Railscasts are awesome.

When I try to add a task, it works except that for some reason, the HTML that's inserted is putting escape characters for the first < and the last >, which results in showing the HTML code (as if displaying a text paragraph) in the browser rather than the input field.

Here is an example of what's inserted:

&lt;input id="continuum_stage_attributes__name"
name="continuum[stage_attributes][][name]" size="30" type="text" /&gt;

Any ideas?

Avatar

I have little experience with SVN but I have never setup a repository before. This railscast was very helpful. The use Windows for local development and a Linux server. I had no problems in following the steps.

Question: If I install a plugin that is specific to windows (like Rmagik) , would the plugin store any windows specific files in the vendor folder. If yes then I will have to exclude that folder from svn as well.

Thanks Ryan

Avatar

Ok If you guys are having the problem i listed above pay attention:

Look at the map.open_id_complete line in your code. You might need to change :controller => "session" to :controller => "sessions"

it should look like this, for me anyway

map.open_id_complete 'session', :controller => "sessions", :action => "create", :requirements => { :method => :get }

I hope this helps people out.

Avatar

I have setup ActionMailer to send mails via Gmail.

Here are the settings:

config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "example.com",
:authentication => :plain,
:user_name => "admin@example.com",
:password => "secret"
}
The "admin" username has an email account setup with a name of "Mailer
Account"

How do I set the From: header so that the user's inbox shows the name on
the account that the mail is sent from instead of the username "admin"
that shows up currently.

How do I get this to happen?

Thanks in advance

Avatar

I'm getting that error too

uninitialized constant SessionController

any idea whats causing this?

Avatar

Why do you prefer mocha over rspec's mocking scheme?

Avatar

My introduction to Rails thus far has been a bit of a disaster. All this talk about agile development and here I am stuck at something so trivial.

Is there any way to do things the old way? To me, it makes much more sense to do the scaffolding based off an existing schema. Speaking without knowing if they have or not, but they should have kept the old functionality. It really didn't make any sense to remove it.

Avatar

I've tried both add_columnname_to_table and AddColumnnameToTable and have gotten the following error messages:

"Couldn't find 'add_dateavailable_to_product' generator"

"Couldn't find 'adddateavailabletoproduct' generator"

Avatar

@Kino: It didn't work for me either when using SQLlite, perhaps because of the "now()" error. Replace "now()" with "current_timestamp" and it worked for me.

Now a general question/request.

So say you have two site-wide announcements at once. Instead of the text that says "Hide this message", I want the system to detect that there are two or more messages and then revise the link text to a plural form if applicable.

I wrote a helper method called announcements_hide_link that deals with the if...then logic but unfortuately I couldn't get count to work except in the model. So to check the count I call:

Announcement.count_all_current_announcements(session[:announcement_hide_time])

which coresponds to this code in the model:

  def self.count_all_current_announcements(hide_time)
    with_scope :find => { :conditions => "starts_at <= current_timestamp AND ends_at >= current_timestamp" } do
      if hide_time.nil?
        count
      else
        count(:conditions => ["updated_at > ? OR starts_at > ?", hide_time, hide_time])
      end
    end
  end

Notice how this all looks a lot alike the code in the current_announcements method?

How can I get rid of this duplicate code?

 

Avatar

Hi Ryan,

First of all, thanks for all your railscasts: wonderful job, helped me a lot !

I'm struggling on an authentication through LDAP, more specifically authenticating as Apple's Open Directory users...

any idea on how to do this ?

Avatar

Thanks Ryan for the excellent railscast, as always.

I am getting an exception when I try to protect the logged_exceptions behind an admin authentication (I am using restful_authentication as well):

/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing': uninitialized constant Rails::Initializer::AuthenticationSystem (NameError)

Avatar

I didn't try exception_logger yet, but it looks promising. I had a quick look to the source and saw, that it provides an RSS feed of logged exceptions - so you can use a feed reader to subscribe to the exception logs of your application.

Avatar

Hi

I think that in #config/environments/production.rb code to require authentication, you need add after: accepts.html { login_required_without_basic }
other line for rjs format:
accepts.js { login_required_without_basic }

Moreover, the plugin requires RedCloth gem for exceptions details.

Avatar

Great video! In relation to testing, is it possible to use virtual attributes within fixtures? How does one get around this?

Avatar

it possible to create more than only one attr_accessor ?? In my case, the "Category equivalent model", has 4 fields.

Avatar

Does anyone know what's to become of the code on dev.rubyonrails.org that didn't get migrated to GitHub? Stuff like plugins/exception_notification and adapters/oracle.

Thanks for the Railscasts!

Avatar

First, thank you!

I have been noodling on how to handle various forms of similar views and think this topic warrants more episodes up here. While it's great that examples are simple by necessity, they may fall down in practice when reality strikes :-)

It's certainly the case that new vs edit forms warrant a form partial, for example. But in almost all cases it seems like there's some conditional logic depending on newness. So this means some logic in the controller, a helper or in the partial via locals, in other partials, etc. But what's the best practice?

For example, I have projects that HABTM resources. A resource can exist without a project, but is often created to be associated to that project when saved. The forms for editing are all one partial, but my Save and Back links are all a little different, and some wording and even UI varies a little depending on context. There's too much repetition to keep it DRY, but enough logic to be hairy.

It would be great to hear more about how things can be kept clean in slightly more complicated cases.

Thanks again for doing what you do.

Tom

Avatar

Is it possible to observe more than one model in a sweeper and have different events carried out depending on which model changes?

For example:

class StateSweeper < ActionController::Caching::Sweeper
  observe State, Foo

  def after_save(state)
    expire_cache(state)
  end
 
  def before_save(foo)
    if foo.someparameter > 1
      expire_cache(state)
    end
  end

  def expire_cache(state)
    expire_page :controller => 'javascripts', :action => 'dynamic_states', :format => 'js'
  end
end

I've tried doing something like the above but i doesn't work - I can't figure out how to differentiate between which model changes, a State model or Foo model - the before_save and after_save functions are triggered whether the model being changed is a State or a Foo ...
I could probable use instanceof? inside each action, but this seems a little messy.

Avatar

What I do for security reasons is putting any admin or reporting pages into a separate application, as described here:

http://www.rorsecurity.info/2008/03/03/intranet-and-admin-security/

This is deescalation, a stolen cookie/session/account means less harm.

Avatar

Nice one, really helpful!

Got it running ^^, just quick and dirty.

for those who had problems with the "secret"

just use:
protect_from_forgery :secret => 'mytinylittleveryultramegalongsecret'

with your secret in the logged_exceptions_controller.rb

and for me i had to use:
helper_method :filtered? and copy that method from the helper file (logged_exceptions_helper) to the controller file in the plugin/lib, because it looks like that the helper is not loading correct.

would be nice, if the logging process itself would be written to the log file ("INSERT INTO ...") for better documentation ... etc etc

Avatar

I'm assuming that there's no option for ASC/DESC ordering is there?

ie: :sort_order => 'ASC'

Avatar

Is it possible to use this same concept to build a list to use with Drag and Drop? I'd like to load the lists and display only those states related to the country, but instead of a select list, the user is dragging and dropping values to create a list of items.

If so, any ideas on how to modify the function countrySelected to create my list elements?

THANKS!

Avatar

I don't see how it became less complex than it was. It becomes even more complex when you're introducing template passing to renderer initializer. I thing this is the one of worst implementations of Presenter.
Further more you're using method missing where it's absolutely not needed. That's how Rails becomes slow.

Avatar

Nice episode!

If - for whichever reason - you don't want to use one of the plugins, there's another possibility that I'm currently using in one of my projects: I symlinked the production.log in the public directory (of course it's password protected) so I can check the log at any time and don't have to log in at the server.

Avatar

@Russ & Jeremy, oh interesting, I hadn't tried this without using cookie session store. It may have something to do with the fact that LoggedExceptionsController doesn't inherit from ApplicationController. You may need to add some specific session info to the controller, check out the readme for an example. I'll do some testing and post what I find.

In many ways I wish this plugin was just a generator which created the controller instead of keeping it buried in a place where it's difficult to change. This would solve these kinds of problems.

Avatar

Hi

all the same as Russ Jones : the problem with the :secret and the love of your work !

I don't use Rails Fast Sessions, but I disabled session store in cookies. Maybe it's something like that.

Avatar

Not to sound ungrateful! Love your stuff of course! I'm on the edge of my seat with each episode. You're one of the reasons I love R(uby|ails)!

Avatar

the repository seems to be down

http://require.errtheblog.com/ -> moving servers. be back soon.

:(

Avatar

Doesn't appear to work with Fast Sessions. http://code.google.com/p/rails-fast-sessions/ No matter what I do I get an error complaining ... "No :secret given to the #protect_from_forgery call" on line #53 of logged_exceptions/index.rhtml ... even though I do have one specified. The errors are logged, but the logged_exception views wont show.

Avatar

I always look forward to seeing a new episode in my Railscasts feed. It's very helpful to know how to simulate the production error environment, and the reporting plugin looks really nice. Keep up the good work!

Avatar

Maybe its a cool idea to use jabber (aka xmpp) for exeption notification to the admin.

Avatar

Thanks for this suggestion -- in my unit tests for this it fails if 'name' in fullname=(name) is a single word. It doubles the word. So you'd get user.fullname = 'Joe' and user.fullname => 'Joe Joe'!

Avatar

In my project, there are many accounts. Each account has many users, but the fragments can be cached for all the users in an account, so putting the account id in the cache identifier is enough to make them unique.

So you could do

<% cache "recent_contacts_#{@account.id}" do %>
<%= render :partial => "/shared/contact", :collection => @contacts %>
<% end %>

Avatar

The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William F. Opdyke and Ralph E. Johnson.[5] Opdyke's Ph.D. thesis[2], published in 1992, also used this term.[3] The term "refactoring" was almost certainly used before then.

Avatar

Of course, no controller only uses the defaults. So make_resourceful can be massively customized, while still keeping your controllers trim and readable.

Avatar

I have to admit I was a little confused about adding vs. the "-a" option vs. the index. And comments (in the Peepcode) about git tracking "content", not "files". Whaaaaa.

An article by Ryan Tomayko cleared it all up for me. It's a little more advanced. You might not need his actual technique, but it illuminates the topic of "adding".

http://tomayko.com/writings/the-thing-about-git

Avatar

I really appreciate this post. This was exactly what I needed, and it has saved me a lot of headache. Thanks for your assistance!

Avatar

Ryan --

Thanks for everything you do.

I wanted to point out an issue with atom_feed_helper in Rails 2.0.2 ... it has a bug or two, which have been fixed now on edge Rails.

If I were to do it again, I would not use atom_feed_helper -- RSS seems to me more widely accepted in various readers. Perhaps being "really simple" is a virtue :-)

Avatar

Is it possible to block such as the one showing fields?

Avatar

Oh this version should also work and simpler...

  def last_name()
self.full_name.split(', ')[0]
  end
  
  def last_name=(x)
lname, fname = self.full_name.split(', ')
self.full_name = [x.strip, fname].join(', ') unless lname == x.strip
  end
  
  def first_name()
self.full_name.split(', ')[1]
  end
  
  def first_name=(x)
lname, fname = self.full_name.split(', ')
self.full_name = [lname, x.strip].join(', ') unless fname == x.strip
  end

Avatar

I was able to figure out what was happening before with the field not auto completing.. but now I'm having another problem.

When I hit the save button the category id isn't being stored in the product database.

the log says that category_name gets passed as a parameter but the relationship isn't saved - a new product is created with a NULL in category_id and a new category is created with a category_name.

Any ideas?