Я изучаю ROR.
Я создал скаффолд для Продуктов, которые создают контроллер. Но когда я редактирую продукт, он не редактирует, а создает новый. Я просто пытаюсь отредактировать продукт, а не создавать его.
STACKOVERFOW ХОЧЕТ МНЕ ПИСАТЬ БОЛЬШЕ, ЧТО Я ПИСАЛ ЭТУ СТРОКУ. Если вы думаете, мне нужно больше описания, пожалуйста, дайте мне знать
Контроллер продукта:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
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
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
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
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:store_id, :product_name, :product_price, :product_description, :product_tag, :sku_code)
end
end
Маршруты:
Rails.application.routes.draw do
resources :products
resources :stores
devise_for :users
match 'users/:id' => 'users#show', via: :get
# or
get 'users/:id' => 'users#show'
# or
resources :users, only: [:show]
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'pages#home'
end
_form.html.erb (продукты)
<%= form_for :product, url: products_path do |f| %>
<%= f.collection_select :store_id, current_user.stores, :id, :store_name, prompt: 'Please select the store of this product' %>
<div class="field">
<%= f.label :product_name %>
<%= f.text_field :product_name %>
</div>
<div class="field">
<%= f.label :product_price %>
<%= f.number_field :product_price %>
</div>
<div class="field">
<%= f.label :product_description %>
<%= f.text_field :product_description %>
</div>
<div class="field">
<%= f.label :product_tag %>
<%= f.text_field :product_tag %>
</div>
<div class="field">
<%= f.label :sku_code %>
<%= f.text_field :sku_code %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
edit.html.erb (продукты)
<h1>Editing Product</h1>
<%= render 'form', product: @product %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>