我在使用Rails项目时遇到了一些麻烦。我正在尝试为新主题进行验证。它应该至少有四个字符的主题,但我收到一个错误的用户。这是我的错误:
2.2.0 :019 > t=Topic.new
=> #<Topic id: nil, name: nil, public: nil, description: nil, created_at: nil, updated_at: nil>
2.2.0 :020 > t.valid?
NoMethodError: undefined method `user' for #<Topic:0x007ff53fb9e268>
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/validations/presence.rb:5:in `validate'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
... 21 levels...
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/commands/rails.rb:6:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/command_wrapper.rb:38:in `call'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:183:in `block in serve'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:156:in `fork'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:156:in `serve'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:131:in `block in run'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:125:in `loop'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application.rb:125:in `run'
from /Users/ericpark/.rvm/gems/ruby-2.2.0/gems/spring-1.3.4/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/ericpark/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/ericpark/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
我的主题控制器:
class TopicsController < ApplicationController
def index
@topics = Topic.all
authorize @topics
end
def new
@topic = Topic.new
authorize @topic
end
def show
@topic = Topic.find(params[:id])
@posts = @topic.posts
authorize @topic
end
def edit
@topic = Topic.find(params[:id])
authorize @topic
end
def create
@topic = Topic.new(topic_params)
authorize @topic
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end
def update
@topic = Topic.find(params[:id])
authorize @topic
if @topic.update_attributes(topic_params)
redirect_to @topic
else
flash[:error] = "Error saving topic. Please try again."
render :edit
end
end
private
def topic_params
params.require(:topic).permit(:name, :description, :public)
end
end
主题模型:
class Topic < ActiveRecord::Base
has_many :posts
belongs_to :user
validates :name, length: {minimum: 5}, presence: true
end
我不确定为什么它声明NoMethodError:未定义的方法'user',我认为主题不需要用户方法,因为它不是主题的属性之一。任何帮助和解释将不胜感激!提前谢谢!
答案 0 :(得分:2)
您可能缺少用户模型。
应用程序/模型/ user.rb
class User < ActiveRecord::Base
has_many :topics
has_many :posts
end