Pav*_*ine 1 ruby ruby-on-rails ruby-on-rails-4
我得到了这个错误:未定义的方法`每个'为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>
Run Code Online (Sandbox Code Playgroud)
我的users_controller.rb
def choose_album_to_add
@albums = Album.all
end
Run Code Online (Sandbox Code Playgroud)
可能有什么不对? 编辑
如我所知,我确信我的视图文件choose_album_to_add.html.erb位于/ app/views/users中.我有一条这样的路线:
将'/ addAlbum /:id'匹配为:'users#choose_album_to_add',来自:'get'
当然,我在浏览器上输入/ addAlbum/45,它正确路由.该视图也是肯定加载的,但问题是我在我的控制器上声明的实例变量无法在我的视图中访问.= [
我已经搭建了四个资源,并且按照这种声明实例变量并在视图中使用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'
Run Code Online (Sandbox Code Playgroud)
当我在Console上运行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">]>
Run Code Online (Sandbox Code Playgroud)
这是我通过scaffold生成的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
Run Code Online (Sandbox Code Playgroud)
任何约会更多?谢谢
小智 5
您的方法def choose_album_to_add是私有的 - 将定义移到private语句上方.
因为choose_album_to_add动作必须是视图使用的公共方法.我想你已经证明当你试图设置时没有调用该方法@albums = 1并且它没有任何区别.
| 归档时间: |
|
| 查看次数: |
12825 次 |
| 最近记录: |