zoukankan      html  css  js  c++  java
  • beginning rails3 note

     

     

    respond_to do |format|

    format.html

    format.xml{render :xml=>@products}

    end

    repsond_to :html,:xml

    respond_with(@products)

    respond_with(@product,location=prudct_url)

    cookies.permanet[:last_product_id]=@product.id

     

    redirect_to [:edit,@product],:notice=>”successfully created product.”

    redirect_to edit_product_path(@product),:noteice=>”successfully created product”

     

     

    Rails is built on the principle of a sared-nothing architecture.

    Shared-nothing means tat no piece of data is sared betwwen te servers tat host the application.

    Storing session data on the server would violate this principle.

    The default cookie-based session storage mechanism “just works” wit no configuration required.

     

    rake db:sessions:create

    rake db:migrate

     

     

    attr_accessor :password

    define an accessor attribute,password,a password method isn’t created automatically by active recored,

    this works like any model attribute ,except that it isn’t persisted to the database wen the model is saved.

     

     

     

    rails g observer Comment

    class CommentObserver < ActiveRecord::Base

        def after_create(comment)

            p “”

        end

    end

     

    before you cann this code in action,you have to tell Rails that you want the CommentServer class to be triggered by the Active Record callbacks of the Comment class.

    To do that,make sure you config/application.rb file looks like bellows & restart your application.

    config.active_record.observers = :comment_observer

     

     

     

     

    class User < ActiveRecord::Base

        has_many :replies,:through=>:articles,:source=>:comments

    end

     

    rails generate migration rename_password_to_hashed_password

    def self.up

    rename_column :users,:password,:hash_passwordend

    end

    rails is not silver bullet!

    scope :where_title,lambda {|term| where(“articles.title like ?”,”%#{term}%”}

    Article.where(“published_at < ? and updated_at > ?”,Time.now,Time.now).to_sql

    Article.where(“title LIKE :searc OR body LIKE :search”,{:search=>”%association%”})

    rake db:setup re-create database and import seed data

    rake db:seed

    db/seeds.rb

    Category.create [{:name=>’programming’},{:name=>’Event’}]

    class User < ActiveRecord::Base

        has_one :profile

        has_many :articles,:order=>’’

        has_many :replies,:through=>:articles,:source=>:comments

    end

    has_one :profile,:class_name=>”Account”,:conditions=>”active=1”,:foreign_key=>”account_id”,:order=>”created_at DESC”,:dependent=>:destroy

    :delete

    :nullify

    user.articles.empty?

    user.articles.clear

    user.article_ids

    return an array of associated aritcle ids.

    user.build_profile(attributes={})

    returns a new profile object that as been instantiated with attributes and linked to user through a foreign key but hasn’t yet been saved

    user.create_profile(attribute={})

    return a new Profile object that as benn instaniated with attributes and linked to user through a foreign key and tat has already been saved

    article.title="Introduction to sql”

    equals

    article.title=(“Introducting to sql”)

    article.new_record?

    article.attributes

    find_by_*(cond)

    find_all_by_*(cond)

    find_by_*_and_*(cond1,cond2)

    find_all_by_*_and_*(cond1,cond2)

    article=Article.first

    article.update_attributes(:title=>”RailsConf”,:published_at=>1.day.ago)

    Article.destroy(4)

    Article.destroy([4,5])

    Article.delete_all

    Article.delete_all(cond)

    article.errors.on(:title)

    article.errors.any?

    article.valid?

    reload!

    reloads the Rails application environment with your console session,

    when you code make changed,you can do this, and it equals re-open rails c

  • 相关阅读:
    svn搭建多版本共存记录
    python中使用redis
    小程序之使用腾讯地图获取经纬度
    vue路由元之进入路由需要用户登录权限功能
    input type="tel" 数字输入框显示圆点
    input在IOS中的聚焦问题
    JS实现手机号码中间4位变星号
    CSS实现div填充剩余高度
    小程序之地图导航
    小程序之点击图片放大预览
  • 原文地址:https://www.cnblogs.com/lexus/p/1929090.html
Copyright © 2011-2022 走看看