更新产品实际上会破坏它

时间:2016-01-21 19:47:23

标签: ruby-on-rails-3

我是Rails的新手,正在创建一个用于学习的演示网络商店应用。 我可以通过rails控制台和url localhost:300 / products / new顺利创建产品。 我的问题是我想要更新它们。 我在new.html.erb和edit.html.erb

中都有_form.html.erb部分渲染

在/ products / id / edit中虽然“更新产品”按钮实际上正在销毁产品而不是更新产品

这是 _form.htlm.erb

  <%= form_for(@product) do |f| %>
    <% 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 class="row">
    <div class="col-sm-4 col-xs-12">
        <div class="field">
                <%= f.label :name %><br>
                <%= f.text_field :name, :class => "input-group input-group-sm" %>
        </div>
        <div class="field">
                <%= f.label :description %><br>
                <%= f.text_area :description %>
        </div>
        <div class="field">
                <%= f.label :image_url %><br>
                <%= f.text_field :image_url %>
        </div>
        <div class="field">
                <%= f.label :color %><br>
                <%= f.text_field :color %>
        </div>
        <div class="actions">
                <%= f.submit %>
        </div>
    </div>
</div>
   

如果您需要更多数据,请告诉我 谢谢, 安娜

更新:这里是我的routes.rb

Rails.application.routes.draw do
resources :products
get 'news/index' => 'news#index', as: :news
get 'store' => 'store#index', as: :store
get 'contact' => 'contact#index', as: :contact
get 'products/edit' => 'products#edit'
get 'products/destroy' => 'products#destroy'
get 'about' => 'about#index', as: :about
get 'landing_page' => 'static_pages#landing_page', as: :landing_page
get 'home/index'
root 'static_pages#landing_page'
resources :orders, only: [:index, :show, :create, :destroy]

我已经在products_controller中找到了它并找到了:

# GET /products/1
# GET /products/1.json
 def show
 end
# GET /products/new
 def new
    @product = Product.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

1 个答案:

答案 0 :(得分:0)

Edit.html.erb还有三个选项:显示|删除|返回删除选项编码为:&lt;%= link_to'删除',@ product.destroy%&gt;我删除了这一行,产品被编辑而不是被取消。