ruby on rails fly

         Rails comes equipped with three environments: test, development, and production. The default environment for the Rails console is development:.这就是为什么我会在Mysql 的db中看到如上三个命名的数据库了。

>> Rails.env
  => "development"
  >> Rails.env.development?
  => true
  >> Rails.env.test?
  => false
      $ rails server --environment production 以产品环境运行   bundle exec rake db:migrate RAILS_ENV=production以产品环境运行少不了要production的数据库

     data as resources that can be created, shown, updated, or destroyed—four actions corresponding to the four fundamental operations POST, GET, PUT, and DELETE defined by the HTTP standard (Box 3.2).数据资源的操作对应着http的POST,GET,PUT,DELETE


<%= @user.name %>, <%= @user.email %> 在erb文件中有@user.name,他是假设@user的存在。操作流程如下,首先在UserController下面

 @user = User.find(params[:id])    Here we’ve used params to retrieve the user id从数据库中找到user.

        This view uses Embedded Ruby to display the user’s name and email address, assuming the existence of an instance variable called @user.

gem 'factory_girl_rails', '4.1.0' 这是用户工厂的gem,专门为制造用户而生的。


Even for a small test suite, the gains in speed from this step can be considerable, and I strongly recommend including Listing 7.11 in your test.rb.

config/environments/test.rb
SampleApp::Application.configure do.
  # Speed up tests by lowering BCrypt's cost function.
  require 'bcrypt'
  silence_warnings do
    BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
  end
end

重置数据库:

$ bundle exec rake db:reset

After resetting the database, on some systems the test database needs to be re-prepared as well:
$ bundle exec rake db:test:prepare


model如何与数据库连接

在我做测试的过程中,可以发现比如在工作台下打入User.count,它自动帮你转为SELECT COUNT(*) FROM 'users‘ 当你打入User.all时,它帮你转为 SELECT `users`.* FROM `users`这就是Model和数据库是直接连在一起的证据。


<%= f.label :name %>
<%= f.text_field :name %>
produces the HTML 这就是html.erb和html相互转化的结果
<label for="user_name">Name</label>
<input id="user_name" name="user[name]" size="30" type="text" />


发送一个Post请求

<form action="/users" class="new_user" id="new_user" method="post">
Here the class and id attributes are largely irrelevant; what’s important is action="/users" and method="post". Together, these constitute instructions to issue an HTTP POST request to the /users URI. We’ll see in the next two sections what effects this has.


params

user = User.new(params[:user])
is equivalent to
@user = User.new(name: "Foo Bar", email: "foo@invalid",

                 password: "foo", password_confirmation: "bar")


复数自动加s(在development环境下要先加载头文件)

>> include ActionView::Helpers::TextHelper
>> pluralize(1, "error")
=> "1 error" 
>> pluralize(5, "error")
=> "5 errors"
Note that we can omit the user_url in the redirect, writing simply redirect_to @user to redirect to the user show page.仅仅只需要redirect_to@user就可以重定向到user_url
def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to @user
    else
      render 'new'
    end
  end
flash就相当于一个flash内存一样,暂时用来读写数据
flash = { success: "It worked!", error: "It failed." }
=> {:success=>"It worked!", error: "It failed."}
>> flash.each do |key, value|
?>   puts "#{key}"
?>   puts "#{value}"
>> end
当然首先得在flash中定义如下的flash
class UsersController < ApplicationController
  .
  .
  .
  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
end


GET: 请求指定的页面信息,并返回实体主体。
POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。
PUT: 从客户端向服务器传送的数据取代指定的文档的内容。
DELETE: 请求服务器删除指定的页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值