Rails button_to current_user正确路径



我可以使用什么路径来显示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

和所有的标准路由

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

Then in products#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)

相关内容

  • 没有找到相关文章

最新更新