rails c
ではデータを作成できるんですが、プログラム上でcreate
のSQL文が発行されなくデータが保存できない状態にいます。
どこがいけないのかご指摘いただけると幸いです。
paramはちゃんと渡っているかと思われます。
SQL文 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)
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 %>
route.rb namespace :admin do resources :posts end
posts_controller.rb class Admin::PostsController < ApplicationController 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) binding.pry if @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(params[:post]) 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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/23 12:12
2019/07/23 12:12
2019/07/23 12:30
2019/07/23 13:08
2019/07/23 13:49 編集
2019/07/23 14:31