关联和Collection_select

时间:2013-04-22 06:16:49

标签: ruby-on-rails

错误:nil的未定义方法`map':NilClass

控制器

def new 
  @book=Book.new
  @subjects=Subject.find(:all)
end

模型

belongs_to :subject
attr_accessible :title, :description, :subject_id
validates_presence_of :title,:description

查看

<table>
    <tr>
        <td><b><%= f.label 'Title' %></b></td>
        <td><%= f.text_field :title %></td>
    </tr>
    <tr>
        <td><b><%= f.label 'Description' %></b></td>
        <td><%= f.text_area :description %></td>
    </tr>
    <tr>
        <td><b><%= f.label 'Subject' %></b></td>
        <td><%= f.collection_select(:subject_id,@subjects,:id,:name) %></td>
    </tr>
    <tr>
        <td><%= f.submit 'Save' %></td>
    </tr>
</table>

当我试图保存而不填写标题和描述时,itz向我显示上述错误。如果我填写标题和描述然后itz工作得很好。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

<h2> Add New Book </h2>
<%= form_for(@book) do |f| %>
<% if @book.errors.any? %>
  <ul>
  <% @book.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
<% end %>


<table>

<tr><td><b><%= f.label 'Title' %></b></td>
<td><%= f.text_field :title %></td></tr>

<tr><td><b><%= f.label 'Description' %></b></td>
<td><%= f.text_area :description %></td></tr>

<tr><td><b><%= f.label 'Subject' %></b></td>
<td><%= f.collection_select(:subject_id,@subjects,:id,:name) %></td></tr>

<tr><td><%= f.submit 'Save' %></td></tr>

 </table>

<% end %>

<%= link_to 'Back', root_url %>

这是我的观看代码。如果我从我的视图代码中删除collection_select,那么itz showin验证错误,如标题不能为空或描述不能为空,如果我不填写任何东西并尝试保存...但当我使用collection_select itz显示我(错误:未定义的方法`map'为nil:NilClass)...但我想要标题或描述等验证错误不能为空。

答案 1 :(得分:0)

错误:      Books#create

中的NoMethodError

显示C:/work/Library/app/views/books/new.html.erb,其中第21行引发:

nil的未定义方法`map':NilClass 提取的来源(第21行):

18:     <td><%= f.text_area :description %></td></tr>
19: 
20: <tr><td><b><%= f.label 'Subject' %></b></td>
21:     <td><%= f.collection_select(:subject_id,@subjects,:id,:name) %></td></tr>
22: 
23: <tr><td><%= f.submit 'Save' %></td></tr>

控制器:       class BooksController&lt; ApplicationController中

before_filter :self_load, :only=>[:edit,:show,:update,:destroy]

def index
 @books=Book.where(:subject_id=>params[:id])
end

def new 
 @book=Book.new
 @subjects=Subject.all
end

def create 
 @book=Book.new(params[:book])
   if @book.save
    redirect_to root_url, :notice=>'New Book has been added'
   else 
    render :action=>'new'
   end
end

def edit
end

def update
 if @book.update_attributes(params[:book])
   redirect_to books_path, :notice=>'Book has been updated'
else
   render :action=>'edit'
end
end

def show
end

def destroy
 @book.destroy
 redirect_to root_url
end

def self_load
 @book=Book.find(params[:id])
end
end

class SubjectsController < ApplicationController

before_filter :self_load, :only=>[:edit,:show,:update,:destroy]

def index
 @subjects=Subject.all
 @books=Book.all
end

def new 
 @subject=Subject.new
end

def create 
 @subject=Subject.new(params[:subject])
if @subject.save
   redirect_to root_url, :notice=>'New subject has been added'
else 
   render :action=>'new'
end
end

def edit
end

def update
 if @subject.update_attributes(params[:subject])
   redirect_to root_url, :notice=>'Subject has been updated'
else
   render :action=>'edit'
end
end

def show
end

def destroy
 @subject.destroy
 redirect_to root_url
end

def self_load
 @subject=Subject.find(params[:id])
end

end

模型:     class Book&lt;的ActiveRecord ::基

 belongs_to :subject

 attr_accessible :title, :description, :subject_id

 validates_presence_of :title,:description

 validates_uniqueness_of :title

 validates_length_of :title, :in=>3..20

 validates_length_of :description, :minimum=>5

end

class Subject < ActiveRecord::Base

 has_many :books, :dependent=> :destroy

 attr_accessible :name

 validates_presence_of :name

 validates_uniqueness_of :name

 validates_length_of :name, :in=>3..10

end

视图:     

添加新书

    &lt;%= form_for(@book)do | f | %GT;     &lt;%if @ book.errors.any? %GT;       
           &lt;%@ book.errors.full_messages.each do | msg | %GT;         
  • &lt;%= msg%&gt;
  •        &lt;%end%&gt;       
    &lt;%end%&gt;

<table>

<tr><td><b><%= f.label 'Title' %></b></td>
 <td><%= f.text_field :title %></td></tr>

<tr><td><b><%= f.label 'Description' %></b></td>
 <td><%= f.text_area :description %></td></tr>

<tr><td><b><%= f.label 'Subject' %></b></td>
 <td><%= f.collection_select(:subject_id,@subjects,:id,:name) %></td></tr>

<tr><td><%= f.submit 'Save' %></td></tr>

</table>

<% end %>

<%= link_to 'Back', root_url %>

当我试图添加一本新书并且保持标题和描述为空并尝试保存时,我得到了未定义的方法地图错误..如果我从我的视图源中删除collection_select然后我试着添加一本新书并保持描述和标题为空并试图保存它,itz showin标题和描述不应该为空...当collection_select在我的视图源中,如果我填写标题和描述并试着保存它.. ..它正在运作

答案 2 :(得分:-1)

在您的模型中,您已明确添加了验证以检查标题和说明。

validates_presence_of :title, :description

如果从模型中删除上述行,则可以在没有这些值的情况下保存。

这是您的生产线的答案(除了提到的错误)。但是你没有向我们展示实际创建对象的控制器(create动作)中的代码。