编辑但变为创建

时间:2019-06-06 02:18:32

标签: ruby-on-rails ruby

我正在学习ROR。

我为创建控制器的产品生成了支架。但是,当我编辑产品时,它不会编辑而是创建一个新产品。我只是想编辑产品,而不是创建产品。

STACKOVERFLOW希望我写更多,所以我写了这行。如果您认为我需要更多说明,请让我知道

产品负责人:

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

2 个答案:

答案 0 :(得分:0)

在您的控制器中喜欢

#GET / products / new

 def new
   @product = Product.new
 end

#GET / products / 1 / edit

def edit
  @product = Product.find_by_id(params[:id])
end

您的编辑链接应该是

<%= link_to 'Edit', edit_product_path(@product) %>

您的edit.html.erb应该像

<%= render :partial => 'form', :locals => {:product => @product} %>

答案 1 :(得分:0)

在控制器的@product = Product.new操作上添加new,并将表单更改为:

<%=  form_for product do |f| %>

product是变量的名称(在此处= render 'form', product: @product中声明),并且rails会使用该对象来推断URL和http方法,您无需在表单上指定url参数