Rails创建发布对象不起作用的方法

时间:2015-10-29 04:49:46

标签: ruby-on-rails ruby ruby-on-rails-3 rubymine

我是使用rails的新手。我创建了一篇文章发布表单,当我点击帖子按钮时,它会将我带到文章页面,但它没有发布我输入的内容。

routes.rb

get 'articles/new' => 'articles#new'
post 'articles' => 'articles#create'

articles_controller.rb

 class ArticlesController < ApplicationController
      def index
        @articles = Article.all
      end

      def new
        @article = Article.new
      end

      def create
        @article = Article.new(article_params)
        if @article.save
          redirect_to '/articles'
        else
          render 'new'
        end
      end

      private

      def article_params
        params.required(:article).permit(:content)
      end
    end

`articles/new.html.erb`:

    <div class="create">
  <div class="container-fluid">


    <%= form_for @article do |t| %>
        <%= t.text_field :title, :placeholder => 'Title', :class => 'article_title'  %>
        <%= t.text_area :body, :placeholder => 'Article', :class => 'article_body' %>
        <%= t.submit 'Post', :class => 'btn-btn-article' %>
<% end %>
      </div>
    </div>  

服务器日志

Started POST "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"c49EXOKfsDP6l2xuquvmUncgTAvXIXpylgx5qK+LvCSrPFvuS37zgR9Cv/BDVwMkCFSYcaWbM+3+qpZazQby8g==", "article"=>{"title"=>"Test", "body"=>"Tsting"}, "commit"=>"Post"}
Unpermitted parameters: title, body
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.2ms)[0m  INSERT INTO `articles` (`created_at`, `updated_at`) VALUES ('2015-10-29 04:43:30', '2015-10-29 04:43:30')
  [1m[36m (11.0ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/articles
Completed 302 Found in 14ms (ActiveRecord: 11.3ms)


Started GET "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#index as HTML
  Rendered articles/index.erb within layouts/application (0.0ms)
Completed 200 OK in 32ms (Views: 31.5ms | ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:17 -0400
Processing by HomeController#home as HTML
  Rendered home/home.erb within layouts/application (0.5ms)
Completed 200 OK in 31ms (Views: 30.1ms | ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:21 -0400
Processing by HomeController#home as HTML
  Rendered home/home.erb within layouts/application (0.2ms)
Completed 200 OK in 30ms (Views: 29.1ms | ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:22 -0400
Processing by HomeController#home as HTML
  Rendered home/home.erb within layouts/application (0.6ms)
Completed 200 OK in 96ms (Views: 94.8ms | ActiveRecord: 0.0ms)


Started GET "/articles/new" for 127.0.0.1 at 2015-10-29 00:51:29 -0400
Processing by ArticlesController#new as HTML
  Rendered articles/new.erb within layouts/application (1.0ms)
Completed 200 OK in 86ms (Views: 85.2ms | ActiveRecord: 0.0ms)

3 个答案:

答案 0 :(得分:1)

  

未经许可的参数:title,body

您应该将article_params更改为

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

答案 1 :(得分:1)

因此,您的服务器日志告诉您问题:

Unpermitted parameters: title, body

您必须将titlebody属性列入白名单,因为禁止在Active Model批量分配中使用操作控制器参数,直到它们被列入白名单。

变化:

  def article_params
    params.required(:article).permit(:content)
  end

要:

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

另外,请查看Strong Parameters

的官方Rails文档

更新

我建议您在routes文件中使用RESTful route

resources :articles

然后,你将有这条路线:

articles GET    /articles(.:format)          articles#index

然后,在控制器的创建操作中,您可以使用articles_path

  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to articles_path
    else
      render 'new'
    end
  end

答案 2 :(得分:1)

它在你的日志中说 -

Unpermitted parameters: title, body

article_params方法更改为

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