Dotol
Dotol

Reputation: 406

Index view of Rails model printing string with all model data

I just finished Rails' Getting Started guide and everything works perfectly except for this mysterious entry that's being printed in the Index view as shown below. I've been trying to find the cause to no avail, neither have I found no suitable terms to Google this issue. 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>

Clients Controller

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

Upvotes: 0

Views: 217

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53028

Remove = from <%= @clients.each do |client| %>. It is outputting result of each which you don't need to print.

In ERB,

  • <%= %>

    executes ruby code as well as outputs the result

    For example: <%= client.name %>

  • <% %>

    executes ruby code but doesn't output the result

    For example: <% @clients.each do |client| %>....<% end %>

Upvotes: 2

Related Questions