问题描述
我对Ruby有点陌生,从头开始写一个程序...我得到一个错误,我不知道它是从哪里来的。我花了一个多小时,我确信这是一个愚蠢的简单。我有其他类似的网页工作正常,但有一些问题的实例变量。这几乎是与相同的问题。
I am a bit new to Ruby and am writing a program from scratch... I'm getting an error and I can't figure out where it is coming from. I've spent over an hour and i'm sure it's something stupidly simple. I have other similar pages that work fine, but there's some issue with the instance variable. It's almost an identical problem as the link provided.
这是我的错误:
Showing C:/Users/kevin/Desktop/ruby_test/sites/simple_cms/app/views/sections/_form.html.erb where line #4 raised:
undefined method `collect' for nil:NilClass
Extracted source (around line #4):
1 <table summary="Section form fields">
2 <tr>
3 <th><%= f.label(:page_id, "Page") %></th>
4 <td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
5 </td>
6 </tr>
7 <tr>
Trace of template inclusion: app/views/sections/edit.html.erb
Rails.root: C:/Users/kevin/Desktop/ruby_test/sites/simple_cms
这是我的表单页面:
<table summary="Section form fields">
<tr>
<th><%= f.label(:page_id, "Page") %></th>
<td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
</td>
</tr>
<tr>
<th><%= f.label(:name) %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.select(:position, 1..@section_count) %></td>
</tr>
<tr>
<th><%= f.label(:visible) %></th>
<td><%= f.check_box(:visible) %></td>
</tr>
<tr>
<th><%= f.label(:content_type) %></th>
<td>
<%= f.radio_button(:content_type, 'text') %> Text
<%= f.radio_button(:content_type, 'HTML') %> HTML
</td>
</tr>
<tr>
<th><%= f.label(:content) %></th>
<td><%= f.text_area(:content, :size => '40x10') %></td>
</tr>
</table>
这是我的控制器:
class SectionsController < ApplicationController
layout "admin"
def index
@sections = Section.sorted
end
def show
@section = Section.find(params[:id])
end
def new
@section = Section.new({:name => "Default"})
@pages = Page.order('position ASC')
@section_count = Section.count + 1
end
def create
@section = Section.new(section_params)
if @section.save
flash[:notice] = "Section created successfully."
redirect_to(:action => 'index')
else
@pages = Page.order('position ASC')
@section_count = Section.count
render('new')
end
end
def edit
@section = Section.find(params[:id])
end
def update
@section = Section.find(params[:id])
if @section.update_attributes(section_params)
flash[:notice] = "Section updated successfully."
redirect_to(:action => 'show', :id => @section.id)
else
@pages = Page.order('position ASC')
@section_count = Section.count
render('edit')
end
end
def delete
@section = Section.find(params[:id])
end
def destroy
section = Section.find(params[:id]).destroy
flash[:notice] = "Section destroyed successfully."
redirect_to(:action => 'index')
end
private
def section_params
params.require(:section).permit(:page_id, :name, :position, :visible, :content_type, :content)
end
end
以下是查看页面(编辑):
Here's the view page (edit):
<% @page_title = "Edit Section" %>
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="sections edit">
<h2>Update Section</h2>
<%= form_for(:section, :url => {:action => 'update', :id => @section.id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Update Section") %>
</div>
<% end %>
</div>
推荐答案
您应该定义 @pages
You should define @pages
and @section_count
variables in appropriate controller action:
def edit
@section = Section.find(params[:id])
@pages = Page.order('position ASC')
@section_count = Section.count
end
您在创建操作时也有错误。当它不创建记录时, @section_count
应为 Section.count + 1
c> new 动作。
You also have a mistake in create action. When it doesn't create a record, @section_count
should be Section.count + 1
as in new
action.
您的控制器也可以拒绝
Your controller could be also refractor to this
这篇关于NoMethodError in Sections#编辑类似的问题到其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!