我在rails控制器中遇到问题,我的索引,显示页面不会出现在我的服务器中,这是我在控制器中的代码:
class GamesController < ApplicationController
def index
@games=Game.all
@render_carousel = true
end
def new
@game=Game.new
end
def create
@game=Game.new(game_params)
if @game.save
flash[:notice] = 'game created'
redirect_to games_path
else
render 'new'
end
end
def edit
@game=Game.find(params[:id])
end
def update
@game=Game.find(params[:id])
if @game.update(game_params)
flash[:notice] = 'game updated'
redirect_to games_path
else
render 'edit'
end
end
def destroy
@game=Game.find(params[:id])
@game.destroy(game_params)
flash[:notice] = 'game deleted'
redirect_to games_path
end
def show
@game=Game.find(params[:id])
end
private
def game_params
params.require(:game).permit(:name, :year, :description, :price, :category_id, :creator_id,:coverpath)
end
end
这是我的索引页面代码:我在link_to行中收到错误,错误在索引和显示页面中
<div id="page-index">
<div class="row">
<div class="col-md-3 col-sm-3">
<div class="thumbnail">
<%= image_tag('cover/un1.jpg')%>
<h4><b>Uncharted</b></h4>
<p>The Uncharted series is one of the best action adventure franchises in recent gaming history,
</p>
<%= link_to 'Read More' ,@game_path(game), class:'btn btn-primary'%>
<div>
</div>
</div>
</div>
答案 0 :(得分:0)
这样做:
<%= link_to 'Read More', game_path(game), class:'btn btn-primary'%>
或者,就是这样:
<%= link_to 'Read More', game, class: 'btn btn-primary'%>
只要game
实际存在于数据库中,两者都应该有效。
循环浏览@games
并显示index.html.erb
文件中的每个游戏链接:
<% @games.each do |game| %>
<%= link_to 'Read More', game_path(game), class:'btn btn-primary'%>
<% end %>