如何向视图添加逻辑?红宝石 on Rails
现在我正在 Rails 中构建一个项目管理应用程序,这里有一些背景信息:
现在我有 2 个模型,一个是用户,另一个是客户端。客户端和用户具有一对一的关系(客户端 -> has_one 和用户 -> Members_to 这意味着外键位于用户表中)
所以我想做的是一旦你添加了一个客户端实际上可以向该客户端添加凭据(添加用户),为此,所有客户端都将显示在该客户端名称旁边的链接,这意味着您实际上可以为该客户端创建凭据。
我不知道该怎么做是,如果您实际上在数据库中有凭据(意味着用户表中有一条包含您的客户端 ID 的记录),则不要显示该链接。
这是我认为可行的,
<% for client in @client%>
<h5>
<h4><%= client.id %></h4>
<a href="/clients/<%= client.id %>"><%= client.name %></a>
<% for user in @user %>
<% if user.client_id = client.id %>
<a href="/clients/<%= client.id %>/user/new">Credentials</a>
<%end%>
<% end %>
</h5>
<% end %>
这是控制器:
def index
@client = Client.find_all_by_admin(0)
@user = User.find(:all)
end
但它只是将链接放置在用户表中每条记录的次数。有什么帮助吗?
Right now I'm building a project management app in rails, here is some background info:
Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)
So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.
What i can't figure it out how to do is, that if you actually have credentials in the database (meaning that there's a record in the users table with your client id) then don't display that link.
Here's what i thought that would work
<% for client in @client%>
<h5>
<h4><%= client.id %></h4>
<a href="/clients/<%= client.id %>"><%= client.name %></a>
<% for user in @user %>
<% if user.client_id = client.id %>
<a href="/clients/<%= client.id %>/user/new">Credentials</a>
<%end%>
<% end %>
</h5>
<% end %>
And here's the controller:
def index
@client = Client.find_all_by_admin(0)
@user = User.find(:all)
end
but instead it just puts the link the amount of times per records in the user table. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行以下
编辑
更好的解决方案,仅让还没有用户的客户端进入控制器:
You can do the following
Edit
Better solution, getting in the controller only the clients who doesn't have a user yet:
只是为了添加 j. 的答案,您确实应该使用
link_to
视图助手而不是硬编码 URL。Just to add to j.'s answer, you really should use
link_to
view helper instead of hardcoding URLs.我有一段时间没有使用 Rails,但有两件事:
I haven't worked with Rails in sometime, but two things: