Rails模型打印字符串的索引视图,包含所有模型数据

时间:2017-05-04 00:55:46

标签: ruby-on-rails model

我刚刚完成了Rails' Getting Started guide除了在索引视图中打印的这个神秘条目外,一切都很完美,如下所示。我一直试图找到原因无济于事,我也没有找到合适的条款给谷歌这个问题。 enter image description here

Index.html.erb

<h1>Index</h1>
<%= link_to 'New Client', new_client_path %>
<table>
    <tr>
        <th>Name</th>
        <th>Issued On</th>
        <th>Notes</th>
        <th>Finished?</th>
        <th>Payments</th>
    </tr>
    <%= @clients.each do |client| %>
        <tr>
            <td><%= client.name %></td>
            <td><%= client.date %></td>
            <td><%= client.note %></td>
            <td><%= client.finished %></td>
            <td><%= client.payment %></td>
            <td><%= link_to 'Show', client_path(client) %></td>
            <td><%= link_to 'Edit', edit_client_path(client) %></td>
            <td><%= link_to 'Destroy', client_path(client),
                method: :delete,
                data: { confirm: 'This client will be permanentally deleted, do you want to continue?' } %></td>
        </tr>
    <% end %>
</table>

客户端控制器

class ClientsController < ApplicationController

    def index
        @clients = Client.all
    end

    def show
        @client = Client.find(params[:id])
    end

    def new
        @client = Client.new
    end

    def edit
        @client = Client.find(params[:id])
    end

    def create 
        @client = Client.new(client_params)

        if @client.save
            redirect_to @client
        else
            render 'new'
        end
    end

    def update
        @client = Client.find(params[:id])

        if @client.update(client_params)
            redirect_to @client
        else
            render 'edit'
        end
    end

    def destroy
        @client = Client.find(params[id])
        @client.destroy

        redirect_to clients_path
    end

    private

        def client_params
            params.require(:client).permit(:name, :date, :note, :finished, :payment)
        end
end

1 个答案:

答案 0 :(得分:2)

=移除<%= @clients.each do |client| %>。输出您不需要打印的each的结果。

在再培训局,

  • <%= %>

    执行ruby代码并输出结果

    例如: <%= client.name %>

  • <% %>

    执行ruby代码但不输出结果

    例如: <% @clients.each do |client| %>....<% end %>