วิธีการดำเนินการคือ :id พร้อม link_to?

เมื่อฉันพยายามสมัครรับข้อมูลผลิตภัณฑ์โดยการคลิกลิงก์:

<%= link_to "Subscribe", :controller => "products", :action => "subscribe_product", :id => product.id, :method => :post %>

ฉันได้รับข้อผิดพลาดนี้และสังเกตเห็นว่าพารามิเตอร์ไม่ถูกต้อง

ActiveRecord::RecordNotFound in ProductsController#show

Couldn't find Product with id=subscribe_product

{"id"=>"subscribe_product", "method"=>"post"}

วิธีการ Subscribe_product ของฉันใน ProductsController ของฉันคือ:

def subscribe_product
  @product = Product.find(params[:id])
  @product.subscriptions.create(:subscriber_id => current_user.id)
end

เส้นทางของฉัน:

resources :products do
  post :subscribe_product, :on => :collection
end

เหล่านี้คือสมาคม:

class User
  has_many :products
  has_many :subscriptions, :foreign_key => :subscriber_id

class Product
  belongs_to :user
  has_many :subscriptions, :as => :subscribable

class Subscriptions
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true

ผู้ใช้สมัครสมาชิกในคอนโทรลเลอร์อื่น:

PagesController
  def index
   @product_history = current_user.products
  end
end

pages/index.html.erb

<% for product in @product_history %>
  <%= product.name %>
  <%= product.price %>
  <%= link_to "Subscribe", :controller => "products", :action => "subscribe_product", :id => product.id, :method => :post %>
<% end %>

เหตุใดวิธีดำเนินการของฉันจึงถูกมองว่าเป็น ID แทน


person LearningRoR    schedule 06.04.2012    source แหล่งที่มา


คำตอบ (3)


เนื่องจากคุณกำลังขับผ่าน id เส้นทาง subscribe_product จึงควรเป็นเส้นทาง member ลองสิ่งนี้และแจ้งให้เราทราบว่าคุณได้รับอะไร:

resources :products do
  member do
    post 'subscribe_product'
  end
end

ในคอนโทรลเลอร์ (เพื่อรับคุณสมบัติที่ไม่สามารถกำหนดได้จำนวนมาก):

def subscribe_product
  @product = Product.find(params[:id])
  subscription = Subscription.new
  subscription.subscriber_id = current_user.id
  @product.subscriptions << subscription
end
person tsherif    schedule 06.04.2012
comment
เมื่อใช้ link_to แรกของฉัน: <%= link_to "Subscribe", :controller => "products", :action => "subscribe_product", :id => product.id, :method => :post %> มันทำให้เกิดข้อผิดพลาด - No route matches [GET] "/products/5/subscribe_product" - person LearningRoR; 06.04.2012
comment
ฉันคิดว่าลิงก์ของคุณต้องมีลักษณะเช่นนี้ (แฮชแยกสำหรับเส้นทาง): <%= link_to "Subscribe", {:controller => "products", :action => "subscribe_product", :id => product.id}, :method => :post %> หากไม่ได้ผล คุณสามารถเรียกใช้ rake routes | grep subscribe_product และแจ้งให้เราทราบว่าคุณจะได้อะไร - person tsherif; 06.04.2012
comment
ฉันคิดว่ามันใช้งานได้ แต่ฉันสังเกตเห็นว่าฉันมี Can't mass-assign protected attributes: subscriber_id, subscribable_type ดังนั้นฉันจึงไม่แน่ใจ ฉันไม่ต้องการให้สิ่งเหล่านี้มีจำหน่ายเป็นจำนวนมาก ตอนนี้ฉันได้รับ Template Missing แต่ฉันสามารถเปลี่ยนเส้นทางได้อย่างมีประสิทธิภาพ - person LearningRoR; 06.04.2012
comment
นั่นก็หมายความว่าคุณไม่สามารถสร้างมันขึ้นมาด้วยแฮชในคอนโทรลเลอร์ของคุณได้ ฉันจะอัปเดตคำตอบเพื่อแสดงให้คุณเห็นว่าควรทำอย่างไร - person tsherif; 06.04.2012

พยายาม :

resources :products do
  post :subscribe_product, :on => :member
end

มันจะสร้างเส้นทางเช่น:

subscribe_product_product POST     /product/:id/subscribe_product(.:format)   {:action=>"subscribe_product", :controller=>"products"}

และใช้เส้นทางเหมือนใน view :

subscribe_products_path(product.id)
person Community    schedule 06.04.2012

โปรดลองสิ่งนี้ เปลี่ยนเส้นทางของคุณเป็น:

resources :products do
  post :subscribe
end

จากนั้นเปลี่ยนลิงค์ของคุณเช่น:

<%= link_to "Subscribe", subscribe_products_path(:id => product.id), :method => :post %>
person Spyros    schedule 06.04.2012
comment
สิ่งนี้ทำให้ฉันมี NoMethodError เนื่องจากผลิตภัณฑ์ที่กำลังดูอยู่ในคอนโทรลเลอร์อื่น ไม่แน่ใจว่าจะจัดการกับเรื่องนี้อย่างไร ฉันจะแก้ไขคำตอบของฉันและแสดงตัวควบคุมและมุมมอง - person LearningRoR; 06.04.2012
comment
ไม่มีข้อผิดพลาดวิธีใด? ด้วยวิธีไหน? โปรดลอง Subscribe_products_path แทน ฉันทำผิดพลาดในลำดับของเส้นทางที่มีชื่อ :P - person Spyros; 06.04.2012
comment
ฉันต้องทำมัน post :subscribe, :on => :collection แต่แล้วฉันก็ได้รับข้อผิดพลาด Couldn't find Product without an ID - person LearningRoR; 06.04.2012
comment
Template missing นั่นเป็นอันใหม่! คิดว่าเมธอด subscribe เป็นมุมมองแล้ว - person LearningRoR; 06.04.2012
comment
นั่นเป็นเพราะคุณไม่ได้เปลี่ยนเส้นทางที่ไหนสักแห่งในวิธีการควบคุมการสมัครสมาชิกของคุณ หากคุณเพิ่ม 'redirect_to products_path' ระบบจะเปลี่ยนเส้นทางไปยังหน้านั้น ฉันขอแนะนำให้อ่านหนังสือเกี่ยวกับ Rails เป็นอย่างยิ่งเพื่อให้เข้าใจวิธีการทำงานได้ดีขึ้น - person Spyros; 06.04.2012