2
我在這裏發佈,因爲我找不到任何人有這個錯誤。我使用Rails 4.0.2,當我嘗試保存我的形式(它採用了collection_check_box)它給了我這個消息:未定義的方法`名稱'爲零:NilClass保存
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/projeto_controller.rb:34:in `block in create'
app/controllers/projeto_controller.rb:33:in `create'
我有點丟在這裏,因爲我沒有任何屬性命名爲name
。 在這裏我的控制器和模型。
class Projeto < ActiveRecord::Base
belongs_to :usuario
has_many :projeto_temas
has_many :temas, through: :projeto_temas
accepts_nested_attributes_for :temas
validates_presence_of :titulo, :orgao_financiador, :periodo_inicio, :periodo_fim
end
class ProjetoController < ApplicationController
# GET /projeto/new
def new
@projeto = Projeto.new
render :layout => 'application_cadastro'
end
# POST /projeto
# POST /projeto.json
def create
@projeto = Projeto.new(projeto_params)
respond_to do |format|
if @projeto.save
format.html { redirect_to @projeto, notice: 'Projeto was successfully created.' }
format.json { render action: 'show', status: :created, location: @projeto }
else
format.html { render action: 'new' }
format.json { render json: @projeto.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projeto/1
# PATCH/PUT /projeto/1.json
def update
respond_to do |format|
if @projeto.update(projeto_params)
format.html { redirect_to @projeto, notice: 'Projeto was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @projeto.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_projeto
@projeto = Projeto.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def projeto_params
params.require(:projeto).permit(:titulo, :usuario_id, :orgao_financiador, :periodo_inicio, :periodo_fim, :resumo, :temas_ids => [])
end
end
和Temas
模型。
class Temas < ActiveRecord::Base
has_many :relacionamento_temas_pai, class_name: RelacaoTemas, foreign_key: :temas_pai_id
belongs_to :relacionamento_temas_filho, class_name: RelacaoTemas, foreign_key: :temas_filho_id
has_and_belongs_to_many :projeto
accepts_nested_attributes_for :relacionamento_temas_pai
validates :nome, presence: true
end
觀點與ckeck_box
<div class="presquisadores-preview-action">
<div class="temas-projetos-checkbox">
<%= f.collection_check_boxes :temas_ids, Temas.all, :id, :nome %>
</div>
</div>
它有哪些是葡萄牙語的名字命名nome
一個屬性,所以它應該不會影響任何東西的一部分。
在此先感謝您的幫助,真的迷失了方向,不知道該怎麼做。
--edit
所以......大量的研究後,我並沒有發現問題。其實我正在研究一些rails文檔,並看到activerecord
版本的一些問題,所以我更新到rails 4.2.0,現在問題已經消失。 我仍然不知道是什麼原因造成的,但現在我的表單正常保存。 感謝所有幫助的人們
如果不讀取所有的代碼,'nil:NilClass on save'的undefined method'name'意味着你做了'something.name'。你的意思是做'something.nome'嗎?轉到第34行projeto_controller.rb並檢查拼寫。 – binarymason
http://guides.rubyonrails.org/i18n.html它幫助你重寫默認參數 –
@ m8ss我在項目中搜索過,沒有用'.name'找到任何東西,我甚至刪除了所有的寶石和讓標準來看看其中一個是否導致這個錯誤。 – danfaiole