Rails4:如何在has_one和belongs_to关联中显示数据

时间:2015-09-13 03:05:45

标签: ruby-on-rails ruby ruby-on-rails-4

虽然我要做的是在详细信息表格中显示name,但会显示#<Details:0x44e5f00>。 我该如何修改我的代码?

\分贝\ schema.rb

  create_table "details", force: true do |t|
    t.integer  "title_id"
    t.integer  "day"
    t.string   "name"
    t.string   "detail"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "title", force: true do |t|
    t.date     "title_date"
    t.string   "title"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "articles", force: true do |t|
    t.integer  "title_id"
    t.integer  "day"
    t.integer  "category"
    t.string   "title"
    t.string   "contents"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

\模型\ title.rb

    has_one :details

\模型\ detail.rb

    belongs_to :title

\视图\ titlees \ show.html.erb

以下是有关行。 <%= Detail.find_by(title_id: (params[:id]), day: inx) %>

<% @article = Article.where(title_id: (params[:id])).order(day: :asc) %>

<div class="row">

  <% for inx in 1..@article.maximum(:day) %>

    <div class="day">

        <div class="row">
          <div class="col-md-12">
            <h3>Day <%= inx %>&nbsp;&nbsp;</h3>
          </div> <!-- md12 -->
        </div> <!-- row -->

        <% @article.where(:day => inx).each do |d| %>
          <div class="row">
              <div>
                <strong><%= d.title %></strong>
                <div class="row">
                    <%= d.contents %>
                </div> <!-- row -->
              </div>
          </div> <!-- row -->
        <% end %>

        <div class="row">
          <div>
            <%= Detail.find_by(title_id: (params[:id]), day: inx) %>
          </div>
        </div>

    </div> <!-- day -->
    <br>
  <% end %> <!-- for -->

 </div> <!-- row -->           

2 个答案:

答案 0 :(得分:1)

忽略您违反了所有Rails约定,以下是我的建议

首先改变这个

has_one :details

到这个

has_one :detail

而不是在您的视图中更新您的代码

<% @article = Article.includes(:detail).where(title_id: (params[:id])).order(day: :asc) %>

<div class="row">

  <% for inx in 1..@article.maximum(:day) %>

<div class="day">

    <div class="row">
      <div class="col-md-12">
        <h3>Day <%= inx %>&nbsp;&nbsp;</h3>
      </div> <!-- md12 -->
    </div> <!-- row -->

    <% @article.where(:day => inx).each do |d| %>
      <div class="row">
          <div>
            <strong><%= d.title %></strong>
            <div class="row">
                <%= d.contents %>
            </div> <!-- row -->
          </div>
      </div> <!-- row -->
    <% end %>

    <div class="row">
      <div>
        <%= @article.detail.name %>
      </div>
    </div>

</div> <!-- day -->
<br>
<% end %> <!-- for -->

</div> <!-- row -->   

答案 1 :(得分:-1)

您的代码几乎正确,您只错过了name属性, 如果你想显示它,你必须在从数据库获得记录后调用name属性

e.g。 : @detail = Detail.find_by(title_id: (params[:id]), day: inx)

Id = @detail.id

name = @detail.name

    <div class="row">
      <div>
        <%= Detail.find_by(title_id: (params[:id]), day: inx).name %>
      </div>
    </div>

如果你能重构你的代码会更好