質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

248閲覧

データの編集。edit.updateアクション

takeke

総合スコア60

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2018/04/15 12:50

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 %>

参考書見ながらここら辺は書いたつもりです
商品のコントローラー自体は結構前に作って、最近はカテゴリーを追加したのですがそこで編集がうまくできないことに気づきました。カテゴリー追加前までは普通にできていたような気もするんですが記憶が曖昧です。
ちなみにカテゴリー機能の方の編集も同じように編集できないでいます。コントローラーもうまくいかなかったので商品のコントローラーの主要な部分をそのまま名前変えて使っています。

以上なんですが、どこか編集できない原因などお気づきになられた方いましたらご教授いただけましたら幸いです。宜しくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

フォームの書方を変えただけで解決できました。
<%= form_with(model: product, path: products_path, method: :post, local: true) do |form| %>   

↓これに変更

<%= form_for @product do |form| %>

投稿2018/04/16 08:46

takeke

総合スコア60

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問