为什么Index视图打印整个表?

时间:2015-05-15 15:47:15

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

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

控制器:

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>

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

screen shot of output

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

变化:

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

对此:

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

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

相关问题