浏览 1800 次
锁定老帖子 主题:rails320
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-03-21  

今天的学习记录 :2008-3-20

1 字符串中插入表达式  “”最好
2 generate controller 是应该输入复述形式(对应表名) model 则是单数
3 关系的表达 has_many :works  belongs_to :composer
class Composer < ActiveRecord::Base
ddhas_many :works

4 class Customer < ActiveRecord::Base
ddhas_manyrders,
ddddddddddd:dependent => true,
ddddddddddd:order => "created_at ASC"
end

5
  Define before_create in the Order class as follows:
  def before_create
dd  self.status = "open"
  end

6

  We’ll write open_orders to return an ActiveRecord collection:
  def open_orders
   orders.find(:all, :conditions => "status = 'open'")
  end

7
  def editions
ddddEdition.find_by_sql("SELECT edition_id from editions_works
ddddLEFT JOIN works ON editions_works.work_id = works.id
ddddLEFT JOIN composers ON works.composer_id = composers.id
ddddWHERE (composers.id = #{id})")
  end
8
文件名为 : new.html.erb
<div style='float:right;width:70%;'>
<h1>н¨ÈËÔ±ÐÅÏ¢</h1>

<%= error_messages_for :human %>
<% form_for(@human) do |f| %>
  <p>
    ±àºÅ :<%= f.text_field :code %><br># 表示f 的类型是text_field (这种方法真是不习惯)
    ÐÕÃû :<%= f.text_field :name %>
    <%= f.submit "´´½¨" %>
  </p>
<% end %>
<%= link_to '·µ»Ø', humen_path %>
</div>
这段代码中 form_for 默认的action其实对应的是创建一个humam (约定优于配置,由于是new.html.erb而且是post(提交),所以对应的方法是
2.0 scallfod 自动生成的代码
  # GET /humen/new get方式
  # GET /humen/new.xml
  def new
    @human = Human.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { renderml => @human }
    end
  end

  # POST /humen post方式
  # POST /humen.xml
  def create
    @human = Human.new(params[:human])

    respond_to do |format|
      if @human.save
        flash[:notice] = "#{@human.name}´´½¨³É¹¦!"
        format.html { redirect_to(:action => 'index') }
        format.xml  { renderml => @human, :status => :created, :location => @human }
      else
        format.html { render :action => "new" }
        format.xml  { renderml => @human.errors, :status => :unprocessable_entity }
      end
    end
  end

而如果文件名是edit.html.erb 则会去执行update方法
(解释下 :在new.html.erb 页面打开时(get) 先执行new方法 创建@human ,当提交时(post) 调用create方法 创建human)
<% form_for(@human) do |f| %> 这句 : 可以换成 (f表示变量 @human)
<% form_for :human, @human,:url => { :action => "create" }  do 是一样的

  <% form_for(@post) do |f| %>
    ...
  <% end %>
This will expand to be the same as:

  <% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
    ...
  <% end %>---自己也搞不太清楚了。
我换一种写法
<div style ="float:right;width:70%">
<form action="/shops/new"
method="post">

²ÍÌüÃû³Æ:
<%= text_field :shop, :name %>
<br />
¼Û¸ñ·¶Î§:
<%= text_field :shop, :pricerange %>
<br />
µØÖ· :
<%= text_field :shop, :address %>
<br />

<input type="submit" value="Ìá½»" />

</form>
</div>
在shops控制器中的new方法:
  def new
    case request.method
    when :get
      @shop = Shop.new
    when :post #commit的时候
    @shop = Shop.new(params[:shop])
    @shop.save
    end
  end

这样也可以

9
对象save的时候报 ActionController::InvalidAuthenticityToken 错误 :
加 protect_from_forgerynly => [:create, :update, :destroy] 不知道为什么

10
  rails中的form表单总结
1.表单类型一

<div class=""class="form">
        <%= error_messages_for 'user' %>
        <fieldset>
          <legend>请输入用户信息</legend>
       <% form_for :user do |form| %>
          <p>
            Name: <%= form.text_field :username, :size => 30      %>
          </p>
          <p>
            Password: <%= form.password_field :password, :size => 30 %>
          </p>
            <p>
              ConfirmPassword: <%= form.password_field :password_confirmation, :size => 30 %>
            </p>
            <%= submit_tag "注册", :class => "submit" %>
            <% end %>
        </fieldset>
      </div>
此种是对象绑定的方式,通过表单,跟Model层的对象绑定,通常完成数据的增,改功能。

2.表单类型二

<div class=""class="form">
        <%= error_messages_for 'user' %>
        <fieldset>
          <legend>请输入用户信息</legend>
          <% form_tag do %>
          <p>
            Name: <%= text_field_tag :username, params[:username], :size => 30      %>
          </p>
          <p>
            Password: <%= password_field_tag :password, params[:password], :size => 30 %>
          </p>
            <%= submit_tag "注册", :class => "submit" %>
            <% end %>
        </fieldset>
      </div>
此种主要是为了表单传值
form_for和model绑定,而form_tag不是
form_tag想传什么参数都行,没有约束

11 验证

   class Person < ActiveRecord::Base
    validates_length_of :first_name, :maximum=>30
    validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind"
    validates_length_of :fax, :in => 7..32, :allow_nil => true
    validates_length_of :phone, :in => 7..32, :allow_blank => true
    validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
    validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
    validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
  end

12
generate scaffold Post title:string body:text  生成框架 并且会生成创建表的schema
  执行创建脚本 rake db:migrate  执行 rake db:reset drop表。。
demo 表
  001_create_SmUsers.rb
class CreateSmUsers < ActiveRecord::Migration
  def self.up
    create_table :sm_users do |t|
      t.string :name, :null => false #name字段类型 string 不能为空
      t.string :realname,:password,:email,:department,:telephone,:mobile
      t.text :memo #
      t.boolean :status
      t.timestamps
    end
    add_index :sm_users, :name,:unique=>true #增加sm_users表name字段 唯一索引
  end
 
  def self.down
    remove_index :sm_users, :name
    drop_table :sm_users
  end
end
  ---个人感觉只是一个对mysql比较方便的工具而已 如果数据集库是oracle 不知道会建成什么样子 (可以指定表空间之类的东西么 ?)

13
  map.root :controller => 'posts'  讲posts这个action作为index ,同时要把 public下的index.html删除掉

14
 
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics