【问题标题】:Modify page based on user's products根据用户产品修改页面
【发布时间】:2019-07-01 08:23:29
【问题描述】:

一个用户有很多产品,一个产品只属于一个用户。在用户配置文件中,我想实现这个概念:

  1. 如果用户没有产品,“创建新产品”按钮可用
  2. 如果用户已经有一个商店,“创建新产品”和可以点击以指向产品本身的产品列表
<% if @product.include? current_user.id %>
<%= link_to 'My product', product_path %>
<% end %>
<%= link_to 'Create New Product', new_product_path %>
<%= link_to 'Edit', edit_user_registration_path %>

【问题讨论】:

  • 请阅读Rails Assosiation以获得更好的知识和理解。
  • 你有什么问题?

标签: ruby-on-rails


【解决方案1】:

如果你实现了正确的关系,下面的代码就会起作用

<%current_user.products.present?%>
  <%@current_user.products.map|product|%>
<%= @product.name%>
<button> Create Product </button>
 <%end%>
<%else%>
<button> Create Product </button>
<%end%>
Rails 协会(https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2015-09-11
    相关资源
    最近更新 更多