-1

I'm new to working with rails. I created a article posting form and when I click the post button it brings me to the articles page but it's not posting what I typed.

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>  

Server log:

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)
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
Taylor Gagne
  • 393
  • 2
  • 8
  • 23

3 Answers3

1

Unpermitted parameters: title, body

You should change your article_params to below

def article_params
  params.require(:article).permit(:title, :body)
end
Pavan
  • 33,316
  • 7
  • 50
  • 76
1

So, your server log is telling you the problem:

Unpermitted parameters: title, body

You have to whitelist your title and body attributes as Action Controller parameters are forbidden to be used in Active Model mass assignments until they have been whitelisted.

Change:

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

To:

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

Also, take a look at the official Rails documentation for Strong Parameters

Update

I would suggest you to use RESTful route in your routes file:

resources :articles

Then, you will have this route:

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

Then, in your controller's create action, you can use articles_path:

  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to articles_path
    else
      render 'new'
    end
  end
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
1

It says in your log -

Unpermitted parameters: title, body

Change your article_params method to

def article_params
  params.require(:article).permit(:title, :body, :content)
end
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88