我在rails noob上使用 form_for 删除资源时出现问题。
我正在尝试为用户可以将同一产品的多个实例添加到其列表中的产品创建愿望清单。我没有为列表中的每个产品创建唯一的数据库条目(无论列表中是否已有相同的产品),而是包含了一个计数器列,' row,'当用户将同一产品的倍数添加到他或她的愿望清单时,这会增加。通过相同的逻辑,我希望删除操作首先减少此计数器,直到它达到0,然后从数据库中删除该项。
以下是我所拥有的:
错误消息:
No route matches [DELETE] "/wish"
Routes
Routes match in priority from top to bottom
wishes_path GET /wishes(.:format) wishes#index
POST /wishes(.:format) wishes#create
new_wish_path GET /wishes/new(.:format) wishes#new
edit_wish_path GET /wishes/:id/edit(.:format) wishes#edit
wish_path GET /wishes/:id(.:format) wishes#show
PATCH /wishes/:id(.:format) wishes#update
PUT /wishes/:id(.:format) wishes#update
DELETE /wishes/:id(.:format) wishes#destroy
希望/ index.html.erb
<div class="wishes_body">
<h2>Your Wish-List</h2>
<table>
<thead>
<tr>
<th class="field-label col-md-2 active">
<label>Name</label>
</th>
<th class="col-md-3">Description</th>
<th class="col-md-1">Amount</th>
<th class="col-md-1">Number</th>
<th class="col-md-2">Total</th>
<th colspan="3" class="col-md-3">Remove</th>
</tr>
</thead>
<tbody>
<% @wishes.all.each do |w| %>
<%= render partial: 'wish', object: w %>
<% end %>
</tbody>
</table>
</div>
_wish.html.erb
<tr>
<td class="field-label col-md-2 active">
<label><%= wish.product.name %></label>
</td>
<td class="col-md-3"><%= wish.product.description %></td>
<td class="col-md-1"><%= '%.2f' % (wish.product.amount/100.00) %></td>
<td class="col-md-1"><%= wish.total %></td>
<td class="col-md-2"><%= '%.2f' % ((wish.product.amount/100.00) * wish.total) %></td>
<%= form for(wish_path(wish), :html => { method: 'delete' }) do %>
<td><%= f.label(:i, "How many:") %></td>
<td><%= f.number_field(:i) %></td>
<td><%= f.submit :value => "Remove" %></td>
<% end %>
</tr>
控制器/ wishes.controller.rb
class WishesController < ApplicationController
def index
@wishes = Wish.where("user_id = ?", "#{current_user.id}")
end
def show
@user = current_user
@products = @user.wish_list.products.order("created_at DESC")
end
def create
@product = Product.find(params[:product_id])
if Wish.where(user_id: "#{current_user.id}", product_id: "#{@product.id}").exists?
@wish = Wish.where(user_id: "#{current_user.id}", product_id: "#{@product.id}").first
@wish.total += 1
else
@wish = @product.wishes.new
@wish.user = current_user
@wish.total = 1
end
respond_to do |format|
if @wish.save
format.html { redirect_to action: "index", notice: 'You have added <%= @wish.product %> to your wish list.' }
format.json { render :index, status: :created }
else
format.html { redirect_to @product, alert: 'Wish was not created succesfully.' }
format.json { render json: @wish.errors, status: :unprocessable_entity }
end
end
end
def destroy
case i = params[:i]
when @wish.total > i
@wish.total -= i
respond_to do |format|
format.html { redirect_to action: 'index', notice: 'You removed the item from your wish-list.' }
format.json { head :no_content }
end
when @wish.total == i
@wish.destroy
respond_to do |format|
format.html { redirect_to action: 'index', notice: 'You removed the item from your wish-list.' }
format.json { head :no_content }
end
else
format.html { redirect_to action: 'index', alert: 'You cannot remove more items than you have on your list.' }
format.json { head :no_content }
end
end
end
config.routes.rb
Rails.application.routes.draw do
root 'static_pages#index'
get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/landing_page'
post 'static_pages/thank_you'
resources :orders, only: [:index, :show, :new, :create]
resources :users
devise_for :users, :controllers => { :registrations => "my_devise/registrations" },
:path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout', :sign_up => 'register'}
resources :wishes
resources :products do
resources :comments
end
resources :payments
end
答案 0 :(得分:0)
您应该使用form_tag
而不是form_for
来实现此目的,因为您不会在表单中使用@wish
资源:
<%= form_tag wish_url(wish), method: :delete do %>
等