rails button_to current_user正确的路径

时间:2015-08-30 11:26:03

标签: ruby-on-rails button-to

我可以使用哪条路径来显示current_user自己列出的产品?

= button_to "View My Products", root_path, method: :get, class: "button"

而不是root_path我尝试使用无效的@product.user.all

谢谢!

编辑:

            user GET    /users/:id(.:format)           users#show
     search_products GET    /products/search(.:format)     products#search
            products GET    /products(.:format)            products#index
                     POST   /products(.:format)            products#create
         new_product GET    /products/new(.:format)        products#new
        edit_product GET    /products/:id/edit(.:format)   products#edit
             product GET    /products/:id(.:format)        products#show
                     PATCH  /products/:id(.:format)        products#update
                     PUT    /products/:id(.:format)        products#update
                     DELETE /products/:id(.:format)        products#destroy
       welcome_index GET    /welcome/index(.:format)       welcome#index
                root GET    /                              welcome#index

和所有Devise的标准路线。

1 个答案:

答案 0 :(得分:1)

我认为您需要一个产品控制器索引操作和用户与产品之间的关联

然后在产品#index action

def index
  @products = current_user.products
end

,您的链接将是

= link_to 'View My Products', {controller: :products, action: :index}

如果您对非认证用户的产品#index操作然后使该操作有条件 喜欢 @products = (current_user ? current_user.products : Product.all)