2017-05-04 46 views
0

我剛剛完成了Rails的Getting Started guide,並且除了在Index視圖中打印的這個神祕條目外,其他所有工作都很完美,如下所示。我一直在試圖找到原因,但我沒有找到適合谷歌這個問題的條款。 enter image description hereRails模型打印字符串與所有模型數據的索引視圖

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 
+3

'<(%)= @ clients.each做|客戶| %>'刪除'='閱讀本文http://stackoverflow.com/a/7996827/1297435 –

+0

謝謝我不知道何時和不使用= – Dotol

回答

2

<%= @clients.each do |client| %>刪除=。輸出你不需要打印的each的結果。

在ERB,

  • <%= %>

    執行紅寶石代碼以及作爲結果

    例如輸出:<%= client.name %>

  • <% %>

    執行紅寶石代碼,但不輸出結果

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