Home>
Premise

We are creating a rudimentary blog service with ruby ​​on rails.
The posting function is implemented by linking the article model and the user model.

What I want to solve

When I press the new registration button on the new post screen of the new action, the message "Created" was displayed, but I made a mistake in writing somewhere, and now it returns to the screen when inputting. It was I want the post content to be saved in the database.
It's a rudimentary question, but I'm sorry, but thank you.

Applicable code

schema.rb

ActiveRecord::Schema.define(version: 2020_07_15_083420) do
  create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.string "img"
    t.bigint "user_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["user_id"],

 name: "index_articles_on_user_id"
  end
  create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "nickname"
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"],

 name: "index_users_on_email", unique: true
    t.index ["reset_password_token"],

 name: "index_users_on_reset_password_token", unique: true
  end
end


config/routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: {
        sessions:'users/sessions'
      }
  resources :articles
  root "articles#index"
end

articles_controller.rb

class ArticlesController</pre>
<p><br />
<code>views/articles/new.html.haml</code></p>
<pre><code>.container
  %h1.text-center
    Creating a new blog
.row.justify-content-center
  .col-md-6.mt-5.text-center
    = image_tag'foodie.jpg', alt: ``, height: '500px', width: '500px'
    = render'form', article: @article


views/articles/_form.html.haml

= form_for article do |f|
    .form_group
    = f.label :title, "title", style: "margin-top: 10px;"= f.text_field :title, class:'form_control'
    .form_group
    = f.label :body, "text"
    = f.text_area :body, rows: 10, class:'form_control mt-1'
    %br
    = f.submit "New", class: "btn btn-primary"

application_controller.rb

class ApplicationController</pre>
<strong>What I tried</strong>
<p>・Apply binding.pry and check the contents of the parameter.</p>
<pre><code>def create
   #binding.pry → Result ①
    @article = Article.new(article_params)
    if @article.save
      redirect_to @article, notice:'Created'
    else
      render :new, alert:'Could not create'
    end
   #binding.pry → Result ②
  end


・Result ①

Started POST "/articles" for ::1 at 2020-07-16 18:45:25 +0900
Processing by ArticlesController#create as HTML
  Parameters: {"authenticity_token"=>"4wrxi7CrlBg0MG6JMyna2jpkaU7IjdPzQOvD5r+lTLTi0Z/BOHcETPeJCi6f41gFs4fQpcVpv9WrUcufTukzuQ==", "article"=>""""aaa"">""aaa"title"

 "commit"=>"Create new"}
  User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
From: /Users/laflora/projects/game_plan/app/controllers/articles_controller.rb:21 ArticlesController#create:
    20: def create
 =>21: binding.pry
    22: @article = Article.new(article_params)
    23: if @article.save
    24: redirect_to @article, notice:'Created'
    25: else
    26: render :new, alert:'Could not be created'
    27: end
    28: end
[1] pry(#)>params
=>"4wrxi7CrlBg0MG6JMyna2jpkaU7IjdPzQOvD5r+lTLTi0Z/BOHcETPeJCi6f41gFs4fQpcVpv9WrUcufTukzuQ=="""aaa"article","aaa"article"

 "commit"=>"Create new", "controller"=>"articles", "action"=>"create"} permitted: false>[2] pry(#)>article_params
=>"aaaaaaaa", "body"=>"aaaaaaaa"} permitted: true>[3] pry(#)>Started GET "/articles" for ::1 at 2020-07-16 18:48:12 +0900
[3] pry(#)>exit


・Result ②

Started POST "/articles" for ::1 at 2020-07-16 18:49:30 +0900
Processing by ArticlesController#create as HTML
  Parameters: {"authenticity_token"=>"/wEYGKYPd3IjKc7U4+l80zHUgBm0VkZeydHk5V0xGuwSqwWt+Xy3HKyv4M9YuGOITBnKraE4vlkGEOLZbbGFdQ==", "art"">"bb"">"">>">"

 "commit"=>"Create new"}
  User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendering articles/new.html.haml within layouts/application
  Rendered articles/_form.html.haml (Duration: 1.0ms | Allocations: 516)
  Rendered articles/new.html.haml within layouts/application (Duration: 2.5ms | Allocations: 1215)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered layouts/_header.html.haml (Duration: 0.3ms | Allocations: 124)
  Rendered layouts/_notifications.html.haml (Duration: 0.1ms | Allocations: 21)
From: /Users/laflora/projects/game_plan/app/controllers/articles_controller.rb:27 ArticlesController#create:
    20: def create
    21: @article = Article.new(article_params)
    22: if @article.save
    23: redirect_to @article, notice:'Created'
    24: else
    25: render :new, alert:'Could not be created'
    26: end
 =>27: binding.pry
    28: end
[1] pry(#)>params
=>"/wEYGKYPd3IjKc7U4+l80zHUgBm0VkZeydHk5V0xGuwSqwWt+Xy3HKyv4M9YuGOITBnKraE4vlkGEOLZbbGFicle"=","art" bbbbbbbbbbb"} permitted: false>, "commit"=>"Create new", "controller"=>"articles", "action"=>"create"} permitted: false>[2] pry(#)>article_params
=>"bbbbbbbbb", "body"=>"bbbbbbbbbbb"} permitted: true>[3] pry(#)>Started GET "/articles" for ::1 at 2020-07-16 18:56:06 +0900
[3] pry(#)>exit

・I think the strong parameters also work properly, and the contents of the parameters should not be empty, but I can't find any suspicious description, so I would appreciate it if anyone could support me. ..

Remark

The delete function is working correctly.
2020/07/17 update
I tried to rewrite the strong parameters (article_params) of articles_controller.rb as follows and execute it, but there was no change. When I looked at binding.pry, it seemed that the id was not taken, so I tried rewriting it as :user_id, but the same result was obtained. ..

Why isn't this id pulling?
Thanks for your support.

def article_params
    params.require(:article).permit(:title, :body, :user)
  end
  20: def create
    21: @article = Article.new(article_params)
 =>22: binding.pry
    23: if @article.save
    24: redirect_to @article, notice:'Created'
    25: else
    26: render :new, alert:'Could not be created'
    27: end
    28: end
[1] pry(#)>article_params
=>"Thank you", "body"=>" Nice to meet you."} permitted: true>[2] pry(#)>@article
=>#[3] pry(#)>