Rails Knosk主控制器,早期

class KnoskController < ApplicationController

  def ask
 	# sets and formats the question
	# if one has been posted

	@qtext = params[:question][:qtext]
	if @qtext then
	@qtext.capitalize!
	@qtext.gsub!(" i ", " I ")
	if !@qtext.rindex("?") then @qtext << "?"
	end 

  	# adds the question to the database
	# if one was posted here
  end

  def search
  end

  def answer
  end

  def about
  end

  def options
  end
  
end

Rails 当前用户

protected
  def current_engineer
    Engineer.find(session[:user])
  end
  
  def current_user
    Employee.find(session[:user])
  end

  def check_user_authentication
    unless session[:user]
      redirect_to :controller => 'account', :action => 'login'
    end
  end

Rails 我的初始化购物车

# Initialize shopping cart.
  def initialize_cart
    # #1
    # --
    # if session[:cart_id]
    #  @cart = Cart.find(session[:cart_id])
    # else
    #  @cart = Cart.create
    #  session[:cart_id] = @cart.id
    # end
    
    # #2
    # --
    # if session[:cart]
    #  @cart = session[:cart]
    # else
    #  @cart = Cart.create
    #  session[:cart] = @cart
    # end
    
    # #3
    @cart = session[:cart] ||= Cart.create
  end

Rails 我的搜索书充当雪貂

  def search
    # Book.rebuild_index
    @page_title = 'Search books'
    return unless request.post?
    @books = Book.find_by_contents(params[:q]) if params[:q]
    # flash.now[:notice] = 'No results.' unless @books.size > 0
  end

Rails 更简单的查找器

class << ActiveRecord::Base
  # Use 'Book[params[:id]]'
  # instead of # Let 'Book.find params[:id]'.
  alias_method :[], :find
end

Rails 初始化购物车测试助手

  # CHANGED: Initialize cart.
  def initialize_cart
    @cart = @request.session[:cart] ||= Cart.create
  end

Rails 更新Rails

yes | gem update rails

Rails Rails链接

<%= link_to 'Shop some more!', :action => 'index' %>

Rails 生成迁移

ruby script/generate migration AnyNameYourLike

Rails 耙迁移

rake db:migrate  # runs the applicable migration