如何向视图添加逻辑?红宝石 on Rails

发布于 2024-09-04 22:03:10 字数 974 浏览 5 评论 0原文

现在我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-09-11 22:03:10

您可以执行以下

# controller
def index
   @clients = Client.find_all_by_admin(0, :include => :user)
end

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>

      <% if client.user.blank? %>
         <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
      <% end %>
   </h5>
<% end %> 

编辑

更好的解决方案,仅让还没有用户的客户端进入控制器:

# model
class Client < ActiveRecord::Base
   has_one :user

   named_scope :without_user,
      :conditions => "id NOT IN ( SELECT client_id FROM users )"
end

# controller 
@clients = Client.without_user.find_all_by_admin(0)

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>
      <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
   </h5>
<% end %> 

You can do the following

# controller
def index
   @clients = Client.find_all_by_admin(0, :include => :user)
end

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>

      <% if client.user.blank? %>
         <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
      <% end %>
   </h5>
<% end %> 

Edit

Better solution, getting in the controller only the clients who doesn't have a user yet:

# model
class Client < ActiveRecord::Base
   has_one :user

   named_scope :without_user,
      :conditions => "id NOT IN ( SELECT client_id FROM users )"
end

# controller 
@clients = Client.without_user.find_all_by_admin(0)

# view
<% @clients.each do |client| %>
   <h5>
      <h4><%= client.id %></h4>
      <%= link_to client.name, {:action => 'show', :id => client.id} %>
      <%= link_to "Credentials", 
            {:controller => 'user', :action => 'new', :client_id => client.id} %>
   </h5>
<% end %> 
陪你到最终 2024-09-11 22:03:10

只是为了添加 j. 的答案,您确实应该使用 link_to 视图助手而不是硬编码 URL。

Just to add to j.'s answer, you really should use link_to view helper instead of hardcoding URLs.

一抹微笑 2024-09-11 22:03:10

我有一段时间没有使用 Rails,但有两件事:

  1. 有帮助程序来制作你的 url,很容易犯错误。 (不是我看到的,只是为了让您了解它们......)
  2. 视图不应该有逻辑。尽可能将其放入控制器中。或者在模型中甚至更好。
  3. 最好不要使用“User.find :all”,因为随着应用程序的增长,这会杀死你。
  4. 最好在模型中拥有一个仅获取您将使用的信息的方法。在这种情况下,数据库更好地进行过滤。

I haven't worked with Rails in sometime, but two things:

  1. There are helpers to make your urls, it's easy to make mistakes. (Not that I see one, but just to let you know about them...)
  2. Views should not have logic. As much as you can get away put it in the controller. Or even better in the model.
  3. It's a good practice to not use "User.find :all", as your app grows this will kill you.
  4. It's better to have a method in the model that gets just the info you will use. Databases are better in this case to filter.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文