我正在通过rails介绍指南,但使用'stock'而不是'articles'和'time_detlas'而不是'comments'我的问题是它似乎正确地节省了time_deltas,我想我检查了正确但股票的显示只是在time_deltas表中添加了一个额外的空白行,没有数字显示。有什么建议吗?
股票控制人:
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
时间Delta控制器
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
显示股票
<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)%>
感谢任何帮助,谢谢!
答案 0 :(得分:1)
从time_delta
中删除@<% @stock.time_deltas.each do |time_delta| %>
<tr>
<td><%= time_delta.start %></td>
<td><%= time_delta.length %></td>
</tr>
<% end %>
达
您只需要@才能与您的视图共享此var。例如:如果您将此添加到您的展示操作:@time_deltas = TimeDelta.all
你可以在你的视图中显示time_deltas。
像:
<% @time_deltas.each do |td|%>
<%= td.start%>
<% end %>