RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

@rs, that's a valid concern. However, I think it's acceptable if one page requires a lot of additional styling. No sense in putting all this styling in a global CSS file and requiring it on every other page. Also, if the users are coming from another page they will likely have the global "application.css" stylesheet cached.

Avatar

Ryan, **excellent** work on these screen casts... brilliant, helpful, and bite-sized!

One thing that I am curious about is setting up a rails app that runs like reddit, where the subdomain prefix is used to scope content (programming.reddit.com versus www.reddit.com)... is this possible with routing in rails, or is it better to build multiple app copies? Maybe a screen cast about something like this, if it's applicable?

Thanks again for your hard work!

Avatar

No BB-Code! :(

http://www.rollyo.com/ream88/ruby_on_rails_api

Avatar

Ryan, thank you very much for sharing wisdom clouds :). I can't stop watching it :).

Btw you mentioned that you'd revisit creating a RESTful controller instead of creating the model?

Did you do that?:) I can't find it... the next episode is about SVN.

Peace.

Avatar

I don't like this method because you get two <link ... type="text/css" /> items in your HTML source.
I really don't think that this is an appropriate behavior.

Avatar

Hi Ryan, thank you for the work you've been doing. Lot's of great tips.
On this subject I'd like to see how to handle validation erros, and which would be the best way to display them.

Avatar

What happens when cliquing on the previous comment poster's name ?

Avatar

@ryan bates

hi, i already fix my error.. sorry i didn't see the "==" in the password for the password is equals equals.

now its working and my next step is to connect ito database when i have users table.

Thanks =)

Avatar

@ryan bates

hi, i already fix my error.. sorry i didn't see the "==" in the password for the password is equals equals.

now its working and my next step is to connect ito database when i have users table.

Thanks =)

Avatar

Hi i added the

<% admin? %>

<% end % >
it works great but when i combine it with the super simple authentication then the "must hidden" is not hidden.

any idea?

thanks

Avatar

Can i ask for your help? =) because i have here is a project called "leaveform"

i want to have simple login system. i want to connect it to my users table.

and when they login, they can see the welcome screen.

here is my syntax in app/controllers/application.rb

# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'

  session :session_key => '_leaveform_session_id'
  
  helper_method :admin?
  
  protected
  
  def authorize
    unless admin?
      flash[:error] = "Unauthorize access"
      redirect_to home_path
      false
    end
  end
  
  def admin?
  #create if statement here...
  if session [:password] == 'brian'
    true
    redirect_to home_path
    else
    false
    redirect_to error_path
  end
  end
 
end

here is the syntax of my app/controllers/sessions_controller.rb

class SessionsController < ApplicationController
  
  def create
    if session[:password] = params[:password]
    flash[:notice] = 'succesfully logged in'
    redirect_to home_path
    else
    redirect_to login_path
    end
  end
  
  def destroy
    reset_session
    flash[:notice] = 'successfully logged out'
    redirect_to login_path
  end
end

Avatar

I follow the tutorial but i can login even i type wrong password.

How to fix?

Avatar

@bryce, yep. You can access associations through the model in the partial. It's just like any other view.

Avatar

By far, my most favorite API site is Ruby Search: http://labs.parkerfox.co.uk/ruby.search/

It hits the Rails API and the Ruby API at the same time.

Avatar

Great screen cast Ryan. When you update the html with the @review object, are you able to access it's relational objects? In other words, in the partial that you're rendering, can you access the user table to display: @review.user.human_name ?

Thanks for all the screen casts. They're great.

Bryce

Avatar

Okay, it should be working now. Dreamhost FTP was giving me fits.

@Jeremy & Tristan, thanks, I added it to the show notes.

Avatar

Sorry about that guys, seems the file didn't upload properly. I'm re-uploading it and hopefully it will work.

Avatar

My favorite is: http://www.noobkit.com/

Avatar

Another me to, stops just a minute or so into the video.Download size is 12M

Avatar

Trying to watch it via itunes does not work either.

Avatar

Episode not playing on my macbook, also.

Avatar

It crashes for me too after about 2 minutes. I am running linux.

Avatar

You forgot http://www.railsdocumentation.org, where you can get our patched version of the API (which documents many of the methods in much greater detail!).

Avatar

Something is fishy with this 049_reading_the_api.mov file. -it stops playing after 1 min 44secs.
Too bad , i like this podcast alot.

Avatar

yes, it seems only first file is broken

Avatar

but 'mplayerc.exe' work fine with 049_reading_the_api.m4v

Avatar

my 'mplayerc.exe' stoped too

Avatar

The same problem - only 11Mb was downloaded

Avatar

Actually never mind. Somewhy it downloaded only 11MB :P. Doing redownload :)

Avatar

"Documations that cover..." and crash. Something with video. Crashes in that place.

[qtrle @ 0x8939638]Problem: stream_ptr out of bounds (92128 >= 92038)

Avatar

@Brian, although the message says "successfully logged in" when providing an incorrect password, the user will not have access to the administration capabilities unless the password matches. I did this for simplicity but it can be confusing so you may want to change it.

One way is to move the comparison further up. So instead of checking if the password matches in the "admin?" method, check it inside the "sessions/create" action. Then you can provide an error message when the password is incorrect.

Avatar

Hi, i followed the tutorial and its great but i also try to enter any password and not use the foobar and it can pass.. how to resolve this? =)

on sessions_controller.rb
session[:username] = params[:username]

application.rb
session[:password] == "foobar"

Thanks dude! =)

Avatar

Nice tips! :D

A great source of knowledge (and magic tricks!) for my irbrc has been dotfiles.org

There are a bunch of nice .irbrc posted there, as there are some sweet .bashrc and .vimrc files, which might come in handy to some of you :)

Avatar

If you think exiting and re-entering the console sucks, try

>> reload!

Avatar

You can also do inline logging of SQL right in irb:

http://toolmantim.com/article/2007/2/6/system_wide_script_console_logging

Avatar

Great stuff -- I had no idea that 'y' method existed, very handy!

Re assigns -- you can use a symbol if you call the assigns method: app.assigns(:projects)

Avatar

It's really nice to have it all in one place. Now my code is a bit more DRY.

Thanks

Avatar

Ryan, app.assigns doesn't seem to work with symbols. app.assings['projects'] works!

Avatar

You say your models don't always show up in autocompletion. This is just a case of lazy loading, console doesn't know your classes until you've loaded them (you did Project.new in the beginning, which loaded it). Once console's loaded/required your model, it'll auto-complete

Avatar

Ryan, very useful !

Another cool and powerful thing is that you can load (web)session objects straight into your console.

The how-to on this is written on:

http://tektastic.wordpress.com/2006/12/06/ruby-on-rails-how-to-load-session-objects-into-console/

Avatar

@Mikoangelo, I need to see the code in order to determine what's wrong. But here's not the best place to post that. Instead I recommend making a thread over at railsforum.com. I visit that frequently and would be glad to help over there.

Avatar

I'm having some problems getting it to work. I have the controllers Tips and Programs, which share a HABTM relationship.

In the `Tips#new` view I have a <ul> list of items in Programs, in the style of "HABTM Checkboxes", the railscast.

On the bottom of the page (outside the main form) I have a `form_remote_for :programs, :url => programs_path`. In the `Programs#create` function I create a Program instance, and respond_to format.js. I have a create.js file in the same folder, which renders a partial also used in the `Tips#new` view (btw, can I get DRY on this? It seems I have to have a duplicate file in both view/* folders).

However, whenever I submit the remote form, it shows the JavaScript code directly in the browser, as if it send the wrong Content-Type header. I have tried going over the code several times, but I cannot find the error.

Avatar

Thanks a bunch for this. Very helpful and clear.

Avatar

@Joshua, the "category_ids=" method is one of the few reasons you may want to stick with has_and_belongs_to_many since has_many :throug doesn't have an equivilant. Of course you can still make your own if you're using has_many :through.

That said, many times the join table has extra information in it if you're using has_many :through. In that case you wouldn't want the interface to be simple checkboxes as shown in the earlier episode. You would need some way to manage the extra data in the join.

@Zubin, thanks for the suggestion. I didn't have any reason for omiting the composite primary key and probably should have included it. I usually only add keys and indexes when they become needed, but I think a HABTM join table is a good case where it's always needed.

Avatar

Hi Ryan, I normally add a composite primary key to the habtm join table like this:

add_index :categorizations, [:product_id, :category_id], :primary => true

... and foreign key associations. Is there any reason not to? Or did you omit it to keep it simple?

Avatar

Hey thanks for all the great screencasts, they are so so cool.

There's just one itch I have.

I've actually never used the has_and_belongs_to_many relationship for an app because when I started railing the REST and the has_many through technique was all the buzz.

However in one of your previous screencasts you had a very interesting method for a habtm relationships

collection_singular_ids=

which allowed you to update all the associated records in the join table by passing in a collection of new ids, it seemed that all the deletion of records that weren't in this new list were automatically done for you, if this is what is happening its very useful and I was wondering if the has_many through technique has a similar method?

Avatar

I just found your podcast from iTunes tonight and watched this episode, great stuff! Thank you!

Avatar

@Jack, it's probably best to keep all detailed questions on the forums (which you are already doing, I'm just replying for the benefit of others).

Avatar

Hey great job Ryan this is super helpful. You helped me out on RailsForum and mention self-referencing many-to-many association. Is there a way I could get more help on that somehow (this doesn't seem like the appropriate place)? Perhaps email? or a good link?

Avatar

Congrats sir ! Great tutorial. i've been checkin some of them lately and gosh, it's crystal clear. perfect timing, nice timeline, handy topics, everything's just great. Keep on the good work and bravo again for all that knowledge you share with others.