LoginSignup
0
0

More than 3 years have passed since last update.

create,editのSQL文が発行されないだ!

Last updated at Posted at 2019-07-23
Started GET "/admin/posts/new?utf8=%E2%9C%93&authenticity_token=ml6gSUnqTKTuGQKPd9ZWFdDr8ODFm%2Bx2IuNwOcy%2Fpv%2BbpthhN0OxPRWD3YSGP6LeTSbPfYljLG09WtyO7dQWug%3D%3D&post%5Btitle%5D=fa&post%5Bcontent%5D=fa&post%5Bpost_date%5D=2019-07-17&commit=%E6%8A%95%E7%A8%BF" for ::1 at 2019-07-23 09:06:42 +0900
Processing by Admin::PostsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ml6gSUnqTKTuGQKPd9ZWFdDr8ODFm+x2IuNwOcy/pv+bpthhN0OxPRWD3YSGP6LeTSbPfYljLG09WtyO7dQWug==", "post"=>{"title"=>"fa", "content"=>"fa", "post_date"=>"2019-07-17"}, "commit"=>"投稿"}
  Rendering admin/posts/new.html.erb within layouts/application
  Rendered admin/posts/_form.html.erb (2.9ms)
  Rendered admin/posts/new.html.erb within layouts/application (6.8ms)
Completed 200 OK in 88ms (Views: 85.7ms | ActiveRecord: 0.0ms)

まず、Started GETのURLがおかしいっていうかGETじゃなくてPOSTになるべき

そして、Processing by [コントローラ名#メソッド名] as HTMLは処理が行われたコントローラとメソッド名と取得した形式を示している
参考:ログの見方

createいってないやんん
なのでbinding.pryも使えず、、、、

本来こうゆうログが出て欲しかった

Started POST "/admin/posts" for ::1 at 2019-07-24 08:02:25 +0900
Processing by Admin::PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ugtj2S2144qyRefhnBdSOIxtphgJth7Yp7EERh2xPe5zTvaUoJOQzqjKOq9AGi6thh4MvwTmL5sJa31oXpG1dg==", "post"=>{"title"=>"fa", "content"=>"fa", "post_date"=>"2019-07-10"}, "commit"=>"投稿"}
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  ↳ app/controllers/application_controller.rb:5
   (1.7ms)  BEGIN
  ↳ app/controllers/admin/posts_controller.rb:17
  CACHE User Load (0.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1  [["id", 4], ["LIMIT", 1]]
  ↳ app/controllers/admin/posts_controller.rb:17
  Post Create (0.3ms)  INSERT INTO `posts` (`title`, `content`, `post_date`, `user_id`, `created_at`, `updated_at`) VALUES ('fa', 'a', '2019-07-10 00:00:00', 4, '2019-07-23 23:02:25', '2019-07-23 23:02:25')
  ↳ app/controllers/admin/posts_controller.rb:17
   (1.0ms)  COMMIT
  ↳ app/controllers/admin/posts_controller.rb:17
Redirected to http://localhost:3000/admin/posts

状況をみていく

こんなコントローラを書いた
いたって正常だ

posts_controller.rb
class Admin::PostsController < ApplicationController
  before_action :require_user
  before_action :set_post, :only => [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    unless @post.valid?
      @post.user_id = current_user.id
      @post.save
      redirect_to admin_posts_path, :notice => '記事を登録しました。'
    else
      render :action => "new" 
    end
  end

  def show
  end
  def edit
  end

  def update
    if @post.update_attributes(post_params)
      redirect_to admin_posts_path, :notice => '記事を更新しました。'
    else
      render :action => "edit"
    end    
  end

  def destroy
    @post.delete
    redirect_to admin_posts_path
  end

  private
  def post_params
    params.require(:post).permit(:title, :content, :post_date, :user_id)
  end

  def set_post
    @post = Post.find(params[:id])
  end
end

こんなviewをかいた
form_withの使い方に自信がなかったのでそこが違うのかとurlをパスにしたりform_tagに変えてみたりしてみたが違ったようだ

new.html.erb
<%= form_with model: [:admin, @post], :url => { :action => 'create' }, local: true  do |f| %>
  <div class="form-group">
    <%= f.label :title, '投稿タイトル' ,:class => "label-s"%>
    <%= f.text_field :title, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :content, '投稿内容' %>
    <%= f.text_area :content, class: 'form-control', rows:10 %>
  </div>
  <div class="form-group">
    <div class="input-group date" id="datetimepicker1">
      <%= f.label :post_date, '日付', for: "datetimepicker1", class: "pt-2 pr-2"%>
      <%= f.date_field :post_date, class: 'form-control' %>
    </div>
  </div>

  <%= f.submit '投稿', class: 'btn btn-info btn-block mt-5' %>
<% end %>

原因は、layoutファイルapplication.html.erbに書いていたyeildを囲む部分の記述だ

application.html.erb
    <div class ="justify-content-container-fuluid mt-5 mb-5">
      <div class="container text-center">
        <div class = "col-md-7 container"> #form class
          <%= yield %>
        </div> #/form
      </div>
    </div>

ここの部分がformタグ担っていたのが原因だ。

久しぶりにbootstrapを使って引っかかったところでした。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0