html - 在索引页上显示另外两个不同表的数据

标签 html mysql ruby-on-rails controller

我正在努力从两个单独的表中获取数据以显示在同一索引页上。

我有一个表中的数据,该表显示为列表,该表称为“交易”。

现在,在我的交易模型中,我定义了它有许多“注释”。这是我想要显示同一索引上的数据的另一个表。

我希望每笔交易都通过交易的 id 和 transaction_id 下的注释表中的 id 显示与其关联的注释。

对于我的一生,我还没有弄清楚如何做到这一点。

这是我的交易index.html.erb的样子:

<% @transactions.each do |transaction| %>
<div class="element">
  <%= trash_ico(transaction) %>
  <%= edit_ico(transaction, edit_transaction_path(transaction)) %>
  <%= link_to "<h2><span class=\"el_header\">#{Asset.find(transaction.asset_id).nmc_name} : Assigned to #{User.find(transaction.user_id).name}</span></h2>
    <div class=\"el\">Created: #{transaction.created_at}</div>
    <div class=\"el\">Ended: #{transaction.finished}</div>
    <div class=\"el\">Type: #{if ! transaction.transaction_type.nil? then transaction.transaction_type.name end}</div>
    <div class=\"el\">Status: #{if ! transaction.transaction_status.nil? then transaction.transaction_status.name end}</div>
    <br clear=\"all\" />".html_safe, transaction %>

  <% note.each do |note| %>
    <%= link_to "<div>#{note.notes}</div>".html_safe, note %>

  <% end %>

</div>
<% end %>

这就是为索引定义 Controller 的方式:

def index
  unless params[:asset_id].nil?
    @transactions = Transaction.find_all_by_asset_id(params[:asset_id])
  else
    @transactions = Transaction.all 
  end

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @transactions }
  end
end

我对编程很陌生,但主要是对 Rails 感兴趣。我知道我需要执行的任务是什么,但我不确定执行该任务的语法必须是什么。

我的节目定义有类似的 View 设置。我只是无法在索引上的相应交易下方显示注释。

最佳答案

我在想,类似这样的事情;

<% transaction.notes.each do |note| %>
    <%= link_to "<div>#{note.notes}</div>".html_safe, note %>
<% end %>

自从您声明属于交易的票据以来。 Rails 允许您通过这种方式访问​​它们。

关于html - 在索引页上显示另外两个不同表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24071743/

相关文章:

javascript - PHP 从 MySQLI 获取数据 [删除此]

javascript - 检查 HTML 5 localStorage

html - 使内部div不超过外部div的边界,CSS,HTML

MySQL - 在连接时引用伪表内的外部表

ruby-on-rails - Devise 在 Elastic Beanstalk 上举行 session 时出现未经授权错误的问题。 401 未经授权

javascript - 如何一次记录多个变量?

mysql - SQL中两个表的比较

mysql - 我无法在表中创建外键 - #1005

ruby-on-rails - Rails 3/Ruby 1.9.2 Date.tomorrow 不正确

css - 在 Rails 中如何根据 URL 更改导入的样式