这是我在视图中的代码:show.html.erb
<ul>
<% @bullets.each do |r| %>
<li><%= r.content %></li>
<% end %>
</ul>
这是我在控制器中的代码:users_controller.rb
if cookies[:bullets].nil?
@bullets = Bullet.all.shuffle.first(4)
cookies[:bullets] = @bullets.collect(&:id)
else
@bullets = []
cookies[:bullets].each do |id|
@bullets << Bullet.find(id)
end
end
这将为 nil:NilClass 返回未定义的方法“每个”
<% @bullets.each do |r| %>
我想知道它为什么这样做,以及如何修复它以从名为“bullets”的数据库(sqlite3)表中发布四个随机固定的项目符号内容(列是内容)。
编辑:这是整个控制器:
class StudentsController < ApplicationController
#GET /
def index
@students = Student.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @students }
end
end
#GET /new
def new
@student = Student.new
end
#POST
def create
@student = Student.new(params[:student])
if @student.save
render :file => 'app/views/success'
else
render :file => 'app/views/students/fail'
end
end
#GET /students/{:id}
def show
@student = Student.find_by_url(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @student }
end
end
#BULLETS Randomizing /students/new.html.erb
if cookies[:bullets].nil?
@bullets = Bullet.all.shuffle.first(4)
cookies[:bullets] = @bullets.collect(&:id)
else
@bullets = []
cookies[:bullets].each do |id|
@bullets << Bullet.find(id)
end
end
#GET /students/1/edit
def edit
@student = Student.find_by_url(params[:id])
end
def update
@student = Student.find_by_url(params[:id])
respond_to do |format|
if @student.update_attributes(params[:student])
format.html { redirect_to @student, notice: 'Student was successfully updated.'}
else
format.html { render action: "edit" }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
#DELETE
def destroy
@student = Student.find_by_url(params[:id])
@student.destroy
respond_to do |format|
format.html { redirect_to students_url }
format.json { head :no_content }
end
end
end
编辑#2:像这样?
#GET /students/{:id}
def show
@student = Student.find_by_url(params[:id])
#BULLETS Randomizing /students/show.html.erb
if cookies[:bullets].nil?
@bullets = Bullet.all.shuffle.first(4)
cookies[:bullets] = @bullets.collect(&:id)
else
@bullets = []
cookies[:bullets].each do |id|
@bullets << Bullet.find(id)
end
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @student }
end
end