سؤال

I'm working through the rails intro guide but using 'stocks' instead of 'articles' and 'time_detlas' instead of 'comments' my issue is that it seems to be saving time_deltas correctly, I think I checked that correctly but the show of the stock just adds an extra blank row to the table of time_deltas no numbers show. Any suggestions why?

Stocks controller:

class StocksController < ApplicationController
    def new
        @stock = Stock.new
    end

    def index
        @stocks = Stock.all
    end

    def create
        # XXX Add columns for delta and current standing when we get there
        # they can intiate to nil
        @stock = Stock.new(stock_params)
        if @stock.save
            redirect_to @stock  
        else
            render 'new'
        end
    end

    def show
        @stock = find_stock
        @time_delta = @stock.time_deltas.build
    end

    def edit
        @stock = find_stock
    end

    def update
        @stock = find_stock


        if @stock.update(stock_params)
            redirect_to @stock
        else
            render 'edit'
        end
    end

    def destroy
        @stock = find_stock
        @stock.destroy

        redirect_to stocks_path
    end

    private 
        def stock_params
            params.require(:stock).permit(:name, :hashtag)
        end

        def find_stock
      return Stock.find(params[:id])
    end
end

Time Delta Controller

class TimeDeltasController < ApplicationController

  def create
    @stock = Stock.find(params[:stock_id])
    @time_delta = @stock.time_deltas.create(time_delta_params)
    redirect_to stock_path(@stock)
  end

  private        
    def time_delta_params
      params.require(:time_delta).permit(:start, :length)
    end
end

Show for the stock

<h1> Stock </h1>
<table> 
    <tr>
        <th>Stock</th>
        <th>Hashtag</th>        
    </tr>
        <tr>
            <td><%= @stock.name %></td>
            <td><%= @stock.hashtag %></td>
        </tr>
</table>

<h3>TimeDeltas: </h2>
  <table> 
    <tr>
      <th>Start</th>
      <th>Length</th>    
    </tr>

  <% @stock.time_deltas.each do |time_delta| %>
    <tr>
      <td><%= @time_delta.start %></td>
      <td><%= @time_delta.length %></td>
    </tr>
  <% end %>
</table>

<h3>Add a TimeDelta:</h2>
<%= form_for([@stock, @stock.time_deltas.build]) do |f| %>
  <p>
    <%= f.label :start %><br>
    <%= f.text_field :start %>
  </p>
  <p>
    <%= f.label :length %><br>
    <%= f.text_field :length %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

<%= link_to 'Back', stocks_path%>
<%= link_to 'Edit', edit_stock_path(@stock)%>

Any help is appreciated, thank you!

هل كانت مفيدة؟

المحلول

just remove @ from time_delta

<% @stock.time_deltas.each do |time_delta| %>
        <tr>
          <td><%= time_delta.start %></td>
          <td><%= time_delta.length %></td>
        </tr>
      <% end %>

up

You need @ only to be able to share this var with your view. Eg: If you add this to your show action: @time_deltas = TimeDelta.all you can show time_deltas in your view. like:

<% @time_deltas.each do |td|%>
 <%= td.start%>
<% end %>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top