RailsCasts Pro episodes are now free!

Learn more or hide this

Andrew Porter's Profile

GitHub User: partydrone

Comments by Andrew Porter

Avatar

How does this look when you have fields in both objects? How do create from a single form? For example, I want to track two different expense types: reimbursements and donations.

Reimbursements have a description, donations have name, address, etc., but both have an amount, a receipt, a category, and belong to an account.

I'm trying the polymorphic association approach, but having trouble figuring out how to create a new reimbursement/entry with all the necessary data and associated objects.

Avatar

One answer: pull unicorn_init.sh out of source control and put it in the #{shared_path}/config/ directory and symlink to it like database.yml.

Avatar

How would you deploy to multiple stage environments with your unicorn configuration? Specifically, how to you change:

config/unicorn_init.sh
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E staging"

to

config/unicorn_init.sh
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"

when you run cap staging deploy or cap production deploy, respectively?

Avatar

I found the answer back in episode #82:

app/controllers/application.rb
class ApplicationController < ActionController::Base
  before_filter :authenticate_if_staging

private

  def authenticate_if_staging
    if Rails.env == 'staging' && request.remote_addr !~ /^192.168.0.\d{1,3}$/
      authenticate_or_request_with_http_basic 'Staging' do |name, password|
        name == 'username' && password == 'secret'
      end
    end
  end

end
Avatar

How would I make HTTP basic authentication conditional? For example, I have a staging server at work, and I only want to make people authenticate if they access the server from outside the local network.

app/controllers/application.rb
class ApplicationController << ActionController::Base
  http_basic_authenticate_with :realm => "Staging", :name => "user", :password => "password" if :needs_authentication?

private

  def needs_authentication?
    Rails.env == "staging" && request.remote_addr !~ /^192.168.0.\d{1,3}$/
  end

end

With this, I am always prompted to authenticate—even if needs_authentication? explicitly returns false. Moving http_basic_authenticate_with inside a method doesn't seem to work, either—I get an undefined method error.

Avatar

Short answer (via jquery.com) is: PUT and DELETE are not supported by all browsers.

It's more convenient to go with POST.

Avatar

What other considerations should be taken into account to use the PUT method, instead of POST? Is it as simple as changing the method name?

download_types.js.coffee
jQuery ->
  $('#download_types').sortable
    axis: 'y'
    handle: '.handle'
    update: ->
      $.put($(this).data('update-url'), $(this).sortable('serialize'))
routes.rb
resources :download_types do
  collection do
    put 'sort'
  end
end