ecサイト作ってるのですが、商品の編集ができなくて困っています。
scaffoldは使っていないのですが、編集をするとエラーは吐き出されないのですが前の状態のまま表示され、結局編集していないことになってしまします。
class ProductsController < ApplicationController def index @products = Product.all if params[:title].present? @products = @products.get_by_name params[:title] end end def new @product = Product.new end def create @product = Product.new(product_params) respond_to do |format| if @product.save format.html { redirect_to @product, notice: 'Product was successfully created.' } format.json { render :show, status: :created, location: @product } else format.html { render :new } format.json { render json: @product.errors, status: :unprocessable_entity } end end end def show @product = Product.find_by(id: params[:id]) end def edit @product = Product.find_by(id: params[:id]) end def update @product = Product.find_by(id: params[:id]) respond_to do |format| if @product.update(product_params) format.html { redirect_to @product, notice: 'Product was successfully updated.' } format.json { render :show, status: :ok, location: @product } else format.html { render :edit } format.json { render json: @product.errors, status: :unprocessable_entity } end end end def destroy @product = Product.find_by(id: params[:id]) @product.destroy respond_to do |format| format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } format.json { head :no_content } end end def who_bought @product = Product.find(params[:id]) respond_to do |format| format.atom end end private def set_product @product = Product.find(params[:id]) end def product_params params.require(:product).permit(:title, :description, :image_url, :price, :category_id) end end
class Product < ApplicationRecord has_many :line_items has_many :orders, through: :line_items has_many :favorites, dependent: :destroy belongs_to :category before_destroy :ensure_not_referenced_by_any_line_item validates :title, :description, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} validates :title, uniqueness: true validates :image_url, allow_blank: true, format: { with: %r{.(gif|jpg|png)\z}i, message: 'はgif,jpg,png画像のURLでなければなりません' } scope :get_by_name, ->(title){ where('title like ?',"%#{title}%") } def favorited_by? user favorites.where(user_id: user.id).exists? end private def ensure_not_referenced_by_any_line_item if line_items.empty? return true else errors.add(:base, '品目が存在します') return false end end end
products/edit.html.erb
<%= form_with(model: product, path: products_path, method: :post, local: true) do |form| %> <% if product.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2> <ul> <% product.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div> <%= form.label :title %> <%= form.text_field :title %> </div> <div> <%= form.label :description %> <%= form.text_area :description, rows: 6 %> </div> <div> <%= form.label :image_url %> <%= form.text_field :image_url %> </div> <div> <%= form.label :price %> <%= form.text_field :price %> </div> <div> <%= form.label :category %> <%= form.collection_select :category_id, Category.all, :id, :c_name %> </div> <div> <%= form.submit %> </div> </div> <% end %>
参考書見ながらここら辺は書いたつもりです
商品のコントローラー自体は結構前に作って、最近はカテゴリーを追加したのですがそこで編集がうまくできないことに気づきました。カテゴリー追加前までは普通にできていたような気もするんですが記憶が曖昧です。
ちなみにカテゴリー機能の方の編集も同じように編集できないでいます。コントローラーもうまくいかなかったので商品のコントローラーの主要な部分をそのまま名前変えて使っています。
以上なんですが、どこか編集できない原因などお気づきになられた方いましたらご教授いただけましたら幸いです。宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。