当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails 3 - Updating product actually destroys it

问题描述:

I am new to Rails and was creating a demo web shop app for study.

I could create the products smoothly, both via rails console and by the url localhost:300/products/new.

My problem is when I want to update them.

I have _form.html.erb partial getting rendered both in new.html.erb and edit.html.erb

In /products/id/edit though the button "Update Product" is actually destroying the product instead of updating it

This is the _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>


Please tell me if you need more data

Thanks,

Anna


Updat: here below my 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]


I have pryed in the products_controller and found this:


# 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

网友答案:

The Edit.html.erb had also three options for: Show|Delete|Back The Delete option was coded with : <%= link_to 'Delete', @product.destroy %> I deleted this line and the product got edited instead of being cancelled.

分享给朋友:
您可能感兴趣的文章:
随机阅读: