2016-01-21 72 views
0

我是Rails的新手,並且正在創建一個用於學習的演示網上商店應用。 我可以順利地通過rails控制檯和url localhost:300/products/new創建產品。 我的問題是當我想更新它們。 我有_form.html.erb部分呈現在new.html.erb和edit.html.erb中更新產品實際上會破壞它

在/ products/id /編輯雖然按鈕「更新產品」實際上是銷燬產品而不是更新它

這是_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> 

請告訴我,如果你需要更多的數據 謝謝, 安娜


Updat:這裏我下面的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 
+0

pry in'update action'並提供你的'params' –

+0

你可以在你的問題中包含你的'routes.rb'嗎? – SteveTurczyn

+0

@SteveTurczyn:我已經添加routes.rb到我的問題 –

回答

0

Edit.html.erb還有三個選項:顯示|刪除|返回刪除選項編碼爲:<%= link_to'刪除',@ product.destroy%>我刪除了這一行,產品被編輯被取消。