【问题标题】:Why is Index view printing entire table?为什么索引视图打印整个表格?
【发布时间】:2015-07-27 14:59:31
【问题描述】:

这个应用程序包含一个要提交的表单,目前我正在尝试打印几行表格。这是可行的,但不幸的是,我还得到了整个数据库表属性的一个长字符串。我编写的代码中没有任何内容(我相信)会导致这种情况。我担心这是一些看不见的轨道魔法,任何见解都会很棒!

控制器:

class StudentsController < ApplicationController
  def new
    @student = Student.new
  end

  def create
     student_map = {"student_id" => params[:student_id], "student_name" => params[:student_name],
  "major" => params[:major], "minor" => params[:minor], "other_information" => params[:other_information], 
  "class_year_id" => params[:class_year_id], "hours_st" => params[:hours], "qr_id" => qr_id,}

     if (newStudentRow.save)
        redirect_to action: 'index'
     else
        render action: 'new'
     end
  end

  def index
     @students = Student.all
  end
end

索引视图:

<h1>Students#index</h1>
<p>Find me in app/views/students/index.html.erb</p>

<table>
    <thead>
        <tr>
            <th>Student Name</th>
            <th>ID</th>
        </tr>
    </thead>
    <tbody>
    <%= @students.each do |s| %>
        <tr>
        <td><%= s.student_name %></td>
        <td><%= s.student_id %></td>
    </tr>
    </tbody>
    <% end %>
</table>

输入数据并提交表单后,此链接显示以下输出:

感谢您的帮助!

【问题讨论】:

    标签: html ruby-on-rails sqlite model-view-controller erb


    【解决方案1】:

    变化:

    <%= @students.each do |s| %>
    

    到这里:

    <% @students.each do |s| %>
    

    在 Ruby 中,each 为每个元素执行块并返回数组。让= 输出数组,这就是您看到那个长字符串的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2022-10-15
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多