railsで、 ECサイト風アプリを作っているのですが、1週間ほど手をつけていませんでした。それまでは商品登録、編集が出来ていました。before_actionの存在を知り、編集して動作確認しようとしたところ、商品の登録や、更新に失敗してしまいます。before_actoinのせいでは無いと思うのですが、何が原因で今まで出来たことができなくなっているのかがわからないので、ご教示いただけますと幸いです。
商品のコントローラーを、productsコントローラーとしています。
productsコントローラーがこちらになります。
class ProductsController < ApplicationController before_action :find_post, only:[:show,:edit,:update,:destroy] def home end def index @products =Product.all @cart=Cart.new end def new @product =Product.new end def create @product = current_user.products.new(product_params) if @product.save redirect_to products_path,success:"商品登録に成功しました" else flash.now[:danger]="商品登録に失敗しました" render :new end end def show # @product = Product.find(params[:id]) end def edit #@product = Product.find(params[:id]) end def update # @product = Product.find(params[:id]) @product.update(product_params) if @product.save redirect_to products_path,success:'編集に成功しました' end end def destroy # @product = Product.find(params[:id]) if @product.destroy redirect_to products_path,success:'商品を削除しました' end end private def product_params params.require(:product).permit(:title,:image,:description,:price) end def find_post @product = Product.find(params[:id]) end end
商品を登録するview(products/new)がこちらになります
<div class="topic-new-wrapper" > <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h1 class="text-center">商品の登録</h1> <%= form_for @product do |f| %> <div class="form-group"> <%= f.label "タイトル" %> <%= f.text_field :title%> </div> <div class="form-group"> <%= f.label "¥" %> <%= f.number_field :price%> </div> <div class="form-group"> <%= f.label "画像の登録" %> <%= f.file_field :image%> </div> <div class="form-group"> <%= f.label "商品の概要" %> <%= f.text_area :description, class: 'form-control' %> </div> <%= f.submit '投稿', class: 'btn btn-black btn-block' %> <% end %> </div> </div> </div> </div>
商品(product)のマイグレーションファイルがこちらになります。
class CreateProducts < ActiveRecord::Migration[5.2] def change create_table :products do |t| t.string :title t.string :image t.string :description t.integer :price t.integer :user_id t.integer :quanity t.timestamps end end end
productモデルが、こちらになります。
class Product < ApplicationRecord validates :title,presence: true validates :image,presence: true validates :description,presence: true belongs_to :order belongs_to :user has_many:carts has_many :cart_users, through: :carts, source: 'user' mount_uploader :image, ImageUploader end
プログラムは、<code>を使って質問欄に記入していただけませんか。
回答するときにコピペなどが楽ですので
回答1件
あなたの回答
tips
プレビュー