gpt4 book ai didi

ruby-on-rails - 错误 : undefined method `each' for nil:NilClass

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:18 25 4
gpt4 key购买 nike

我收到此错误:undefined method `each' for nil:NilClass 尝试呈现 choose_album_to_add.html.erb 时:

<table class="table_">
<thead>
<tr>
<th>Album</th>
</tr>
</thead>

<tbody>
<% @albums.each do |album| %>
<tr>
<td><%= album.name %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

我的users_controller.rb

def choose_album_to_add
@albums = Album.all
end

有什么问题吗?编辑

根据要求,我确定我的 View 文件 choose_album_to_add.html.erb 位于/app/views/users 中。我有这样一条路线:

将“/addAlbum/:id”匹配到:“users#choose_album_to_add”,通过:“get”

当然,我在浏览器上输入/addAlbum/45,它会正确路由。 View 也肯定会加载,但问题是我在 Controller 上声明的实例变量无法在我的 View 上访问。 =[

我已经构建了四个资源,它们按照声明实例变量并在 View 中使用 then 的约定工作得很好。我知道有其他方法(比如路过本地人),但我想以这种方式完成这项工作。

尝试访问时来自控制台的日志

Started GET "/addAlbum/50" for 127.0.0.1 at 2014-03-24 09:56:13 -0400
Processing by UsersController#choose_album_to_add as HTML
Parameters: {"id"=>"50"}
Rendered users/choose_album_to_add.html.erb within layouts/application (1.0ms)
Completed 500 Internal Server Error in 3ms

ActionView::Template::Error (undefined method `each' for nil:NilClass):
14: </thead>
15:
16: <tbody>
17: <% @albums.each do |album| %>
18: <tr>
19: <td><%= album.name %></td>
20: <td><%= album.created_on %></td>
app/views/users/choose_album_to_add.html.erb:17:in `_app_views_users_choose_album_to_add_html_erb___3017203676622264637_69974786999340'

当我在控制台上运行 Album.all 时,我得到:

SELECT "albums".* FROM "albums"
=> #<ActiveRecord::Relation [#<Album id: 1, name: "Album1", created_on: "2014-03-17 23:33:00", finishes_on: "2014-03-24 13:16:00", created_at: "2014-03-17 23:33:14", updated_at: "2014-03-24 13:16:32">, #<Album id: 2, name: "Album2", created_on: "2014-03-17 23:33:00", finishes_on: "2014-03-24 13:16:00", created_at: "2014-03-17 23:33:28", updated_at: "2014-03-24 13:16:42">]>

这是我通过脚手架生成的 Rest 方法插入的两个元组。

users_controller.rb

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

# GET /users
# GET /users.json
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)
@user.photographer_id = session[:current_photographer_id]

respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Novo usuário cadastrado com sucesso. Um email foi encaminhado a ele contendo as informações de acesso.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'Dados atualizados com sucesso' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :password, :endereco, :telefone, :cpf)
end

def choose_album_to_add
@albums = Album.all
end

end
end

还有约会吗?谢谢

最佳答案

您的方法 def choose_album_to_add 是私有(private)的 - 将定义移至 private 语句之上。

因为 choose_album_to_add 是一个 Action ,所以必须是 View 使用的公共(public)方法。我认为您已经证明当您尝试设置 @albums = 1 时没有调用该方法并且它没有任何区别。

关于ruby-on-rails - 错误 : undefined method `each' for nil:NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22611929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com