<h1>Book <%= @product.name %></h1>
<% @products.each do |product| %>
<ul>
<%= product.name %>
<%= link_to "Make offer", {controller: "offers", :action => 'create', id: product.id } %>
</ul>
控制器
def show
@product = current_user.products.find(params[:id])
@products = Product.all
end
我的目标是在两个产品之间进行报价。我创建了一个报价模型和相应的方法来制定报价:
class Offer < ActiveRecord::Base
belongs_to :product
belongs_to :exchanger, class_name: "Product", foreign_key: "exchanger_id"
validates :product_id, :exchanger_id, presence: true
def self.request(product, exchanger)
unless product == exchanger or Offer.exists?(product, exchanger)
transaction do
create(product: product, exchanger: exchanger, status: "oczekujace")
create(product: exchanger, exchanger: product, status: "oferta")
end
end
#other methods
end
制作报价是有效的,因为我在控制台中进行了检查。 我的问题在于OffersController:
class OffersController < ApplicationController
before_filter :setup_products
def create
Offer.request(@prod, @exchanger)
redirect_to root_path
end
private
def setup_products
@prod = current_user.products.find(1)
@exchanger = Product.find_by_id(params[:id])
end
end
问题出在以下一行代码(使用商品id不为1的链接可以正常工作):
@prod = current_user.products.find(1)
但我不知道如何在数据库中找到与我的展示页面显示的实际产品相对应的对象,而不仅限于id = 1。
1
for this user. - RAJ