ruby on rails 入门注意事项

ruby on rails 入门注意事项

首先声明:本文不是ruby on rails的入门教程
入门教程可以参考:
http://www.tutorialspoint.com/ruby-on-rails/rails-controllers.htm
http://guides.rubyonrails.org/active_record_basics.html
http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller
本文仅列举入门过程中容易犯的错误
ruby版本:
ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
rails版本:
rails -v
Rails 4.2.6
gem -v
2.5.1
rake –version
rake, version 11.1.2

(1)配置文件冒号后面缺少空格

注意:冒号后面需要空格
ruby on rails 入门注意事项_第1张图片

(2)获取表单参数

以为

params[article]

可以获取表单提交的参数
article前面要加上冒号
正确:

params[:article]

(3)持久化表单参数

最开始以为:

# 增加,持久化到数据库
  def create
    articles = Article.create(params[:article])
    render plain: params[:article].inspect
  end

结果报错:
ActiveModel::ForbiddenAttributesError
正确的:

# 增加,持久化到数据库
  def create
    articles = Article.create(params.require(:article).permit(:title, :text))
    render plain: params[:article].inspect
  end

范例:

def create
   @book = Book.new(book_params)

   if @book.save
      redirect_to :action => 'list'
   else
      @subjects = Subject.all
      render :action => 'new'
   end

end

def book_params
   params.require(:books).permit(:title, :price, :subject_id, :description)
end

(4)传递数据到view

以为这样就可以把model传递到view:

# 条件查询
  def show
    id=params[:id]
article=Article.find_by(id: id)

  end

article前面应该加上at符号
正确的:

 # 条件查询
  def show
    id=params[:id]
    @article=Article.find_by(id: id)
    end

添加记录的表单(app/views/articles/new.html.erb):

<h1>New Articleh1>
<div>
  <%= form_for :article, url: articles_path do |f| %>
      <p>
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      p>

      <p>
        <%= f.label :text %><br>
        <%= f.text_area :text %>
      p>

      <p>
        <%= f.submit %>
      p>
  <% end %>

div>

注意:等号后面需要空格

查询结果的显示页面(app/views/articles/show.html.erb):

<div>查询结果:div>
<div>
  <ul>
    <li>
      <label for="">title:label>
      <label for=""> <%= @article.title %> label>
    li>
    <li>
      <label for="">text:label>
      <label for=""> <%= @article.text %> label>
    li>
  ul>
div>

(5)启动服务

powder link
或者:

./bin/rails server(端口默认是:3000)
停止服务:powder unlink

(6)执行migrate

功能:创建数据表

rake db:migrate

rake db:migrate VERSION=20080906120000

(7)更新记录(保存更新)

我的写法:

def update

    articles = Article.update(params[:id], params.require(:article).permit(:title, :text))
    redirect_to :action => 'list'
  end

最佳实践:

def update
    @article = Article.find(params[:id])
    @article.update(params.require(:article).permit(:title, :text))

    redirect_to :action => 'list'
  end

先通过id查询记录,然后再更新,而不是直接更新

(8)手动指定视图

render template: "articles/show"

类似于springMVC中的:
ruby on rails 入门注意事项_第2张图片
等价于:

render :show

下面几种写法达到的效果是完全相同的:

render :edit
render action: :edit
render "edit"
render "edit.html.erb"
render action: "edit"
render action: "edit.html.erb"
render "books/edit"
render "books/edit.html.erb"
render template: "books/edit"
render template: "books/edit.html.erb"
render "/path/to/rails/app/views/books/edit"
render "/path/to/rails/app/views/books/edit.html.erb"
render file: "/path/to/rails/app/views/books/edit"
render file: "/path/to/rails/app/views/books/edit.html.erb"

(9)浏览器跳转

使用redirect_to

# 增加,持久化到数据库
  def create
    articles = Article.create(params.require(:article).permit(:title, :text))
    # render plain: params[:article].inspect
    redirect_to :action => 'list'
  end

类似于spring MVC中的
response.sendRedirect
ruby on rails 入门注意事项_第3张图片

(10)rubyonrails中好像没有Java Web中的forward跳转

知道在rails中forward跳转的同学麻烦告诉我下

(11)CRUD

增:

Article.create(params.require(:article).permit(:title, :text))

删除:

id=params[:id]
    Article.find(id).destroy;

改:

@book = Book.find(params[:id])
@book.update(book_params)


条件查询

@article=Article.find_by(id: id)

查询所有

@articles=Article.all

(12)指定请求方式

使用method

<%= form_for :article, method: "PUT", url: {action: "update"} do |f| %>

(13)构造表单时推荐使用form_for

we used form_for tag for the form action. It will perform better than form_tag. Why because it will create interaction with the Model easily. Therefore it is better to use form_for tag whenever you need interaction between the model and the form fields.
翻译:
我们使用form_for实现表单提交.它比form_tag 更强大.为什么呢?
因为form_for 可以更方便地与Modeal进行交互(数据绑定).
因此,无论什么时候,你需要在model和表单之间进行数据传递时推荐使用form_for.

参考:
http://www.tutorialspoint.com/ruby-on-rails/rails-controllers.htm
http://guides.rubyonrails.org/layouts_and_rendering.html
http://www.tutorialspoint.com/ruby-on-rails/rails-views.htm
http://hw1287789687.iteye.com/blog/2288230

http://hw1287789687.iteye.com/blog/2288267

你可能感兴趣的:(ruby on rails 入门注意事项)