Ruby on rails 关联和集合\u选择

Ruby on rails 关联和集合\u选择,ruby-on-rails,Ruby On Rails,错误:nil:NilClass的未定义方法“map” 控制器 def new @book=Book.new @subjects=Subject.find(:all) end before_filter :self_load, :only=>[:edit,:show,:update,:destroy] def index @books=Book.where(:subject_id=>params[:id]) end def new @book=Book.new

错误:nil:NilClass的未定义方法“map”

控制器

def new 
  @book=Book.new
  @subjects=Subject.find(:all)
end
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
模型

看法



当我试图保存而不填写标题和描述时,我发现上面的错误。如果我填写了标题和描述,那么它就工作得很好。有人能帮我吗?

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

validates_presence_of :title, :description
如果从模型中删除上述行,则可以不使用这些值进行保存

这是对您的线路的回答(提到的错误除外)。但是您没有向我们显示实际创建对象的控制器(
create
action)中的代码

添加新书
<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 %>
这是我的视图代码。如果我从我的视图代码中删除了集合\u select,则itz showin验证错误,如标题不能为空或说明不能为空,如果我没有填写任何内容并尝试保存。。。但是当我使用集合时,选择itz显示给我(错误:nil:NilClass的未定义方法'map')。。。但我希望验证错误,如标题或描述不能为空

错误: 书籍中的命名错误#创建

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

nil:NilClass的未定义方法“map” 提取的源(第21行附近):

18:
19: 
20: 
21:     
22: 
23: 
控制器: 类BooksController
def new 
  @book=Book.new
  @subjects=Subject.find(:all)
end
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
before\u filter:self\u load,:only=>[:编辑,:显示,:更新,:销毁]
def索引
@books=Book.where(:subject\u id=>params[:id])
结束
def新
@书
@subjects=Subject.all
结束
def创建
@book=book.new(参数[:book])
如果@book.save
将\重定向到根\ url,:notice=>“已添加新书”
其他的
呈现:操作=>'new'
结束
结束
定义编辑
结束
def更新
如果@book.update_属性(参数[:book])
重定向到图书路径:注意=>“图书已更新”
其他的
呈现:操作=>“编辑”
结束
结束
def秀
结束
def销毁
@毁灭
将\u重定向到根\u url
结束
def自动加载
@book=book.find(参数[:id])
结束
结束
类SubjectsController[:编辑,:显示,:更新,:销毁]
def索引
@subjects=Subject.all
@书
结束
def新
@subject=subject.new
结束
def创建
@subject=subject.new(参数[:subject])
如果@subject.save
将\重定向到根\ url,:notice=>“已添加新主题”
其他的
呈现:操作=>'new'
结束
结束
定义编辑
结束
def更新
如果@subject.update_属性(参数[:subject])
将\重定向到根\ url,:notice=>“主题已更新”
其他的
呈现:操作=>“编辑”
结束
结束
def秀
结束
def销毁
@主语:毁灭
将\u重定向到根\u url
结束
def自动加载
@subject=subject.find(参数[:id])
结束
结束
型号: 类目
 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
属于:主题
属性可访问:标题、描述、主题id
验证是否存在:title、:description
验证:标题的唯一性
验证:title,:in=>3..20的长度
验证的长度为:description,:minimum=>5
结束
类主题:销毁
可访问属性:名称
验证是否存在:name
验证名称的唯一性
验证:name,:in=>3..10的长度
结束
视图: 添加新书



当我试图添加一本新书时,我将标题和描述保留为空并尝试保存,我发现未定义的方法映射错误。。如果我从我的视图源中删除所选的集合,然后尝试添加一本新书,并将描述和标题保留为空并尝试保存它,itz将显示标题和描述不应为空。。。当“我的视图源”中存在集合时,如果我输入标题和说明并尝试保存它。。。。如果你不在乎用可读代码提问,你就不能指望有人在意回答。我编辑了你的代码以提高可读性。即使我知道不使用验证也不会出错,但我不明白如果我使用验证,为什么会出错。。。。。。。。。实际上我想使用验证,这样如果标题和描述为空,对象就不会保存…有人能解决上面的问题吗????/我也想使用验证。如果需要更多代码,我将提供。我不明白你的问题。存在验证时,必须输入这些字段。否则您将得到验证错误。我也有错误代码。如果我得到任何错误,它会显示验证错误。我将向你展示我使用rails 3.2.13的完整代码IM。。。我试过你说的代码,但仍然面临同样的问题。。。。。。
 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
<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 %>