Ruby Rails:新 post 未保存在博客上

Ruby on Rails: New post not saving on the blog

我正在做一个练习,在 rails 上用 ruby 创建一个博客。我已经准备好 post 一篇文章的表单,但是一旦我点击提交按钮,我就会被重定向到主页,但文章没有保存。下面是代码

class ArticlesController < ApplicationController
  def index
    @articles = Article.paginate(:page => params[:page], per_page: 5).order('created_at DESC')
  end

  def show
    @article = Article.find(params[:id])
  end

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(title: params.permit[:title], body: params.permit[:body])

    if @article.save
      redirect_to articles, :notice => "Your post has been saved"
    else
      render :create
    end
  end

end

这里是景色create.html.haml

.container
    .row
        .col-xs-9-
            .panel-title
                %h2 Ecrivez votre article
                = form_for @article do |f|
                    = f.text_field :title
                    = f.text_area :body, size: "60x12"
                    = f.submit "Publier"

然后route.rb,不知道能不能帮上忙

TP2::Application.routes.draw do
  resources :articles, only: [:index]

  get 'articles' => 'articles#index'

  get 'articles/:id' => 'articles#show'

  get 'articles/new'

  get 'post' => 'articles#create'

  post 'articles' => 'articles#index'

最后是我尝试 post 一篇文章时控制台显示的内容

Started GET "/post" for 127.0.0.1 at 2016-04-10 14:24:56 +0200
Processing by ArticlesController#create as HTML
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered articles/create.html.haml within layouts/application (1.4ms)
Completed 200 OK in 9ms (Views: 4.9ms | ActiveRecord: 0.4ms)


Started POST "/articles" for 127.0.0.1 at 2016-04-10 14:25:10 +0200
Processing by ArticlesController#index as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FMQmKvZ1t98ZE21VaiBQhm0jKJ9x9BwkXFbh4obfi3Qea0Zax5dgGirfpgcAiQA464GMD2+Qv/eGYrmvEoTZBQ==", "article"=>{"title"=>"Post", "body"=>"New Article test"}, "commit"=>"Publier"}
  Article Load (0.6ms)  SELECT  "articles".* FROM "articles"  ORDER BY created_at DESC LIMIT 5 OFFSET 0
   (0.4ms)  SELECT COUNT(*) FROM "articles"
  Rendered articles/index.html.haml within layouts/application (3.4ms)
Completed 200 OK in 7ms (Views: 5.3ms | ActiveRecord: 1.0ms)

我不明白为什么新文章不会保存。有谁明白为什么?

我会简化路线:

Rails.application.routes.draw do
  root to: 'articles#index' # or where you want for the first page

  resources :articles #will give you the correct path and verbs
end

还有 articles_controller.rb

class ArticlesController < ApplicationController

 ...

  def create
    @article = Article.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: "Article created!" }
      else
        format.html { render action: 'new' }
      end
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :body)
  end
end

因为您正在学习新东西,所以这是您应该用来调试代码的方法:

  • @article.save之前的行上打一个binding.pry(断点)(或者使用其他调试器,你可以在Github上找到它)
  • 重新加载您的页面,输入字段并点击保存
  • 转到rails控制台,在控制台(或@article.valid?)上发出@article.save,它应该return false
  • puts @article.errors,所以你可以知道验证问题是什么

祝你好运:)