Tag: nomethoderror

单击#new&#edit时如何停止NoMethodError?

我很难弄清楚如何将多个控制器的逻辑合并到侧边栏中。 例如,我想在侧边栏中有一个部分,它使用@habits逻辑和一个使用来自goals_controller的@unaccomplished_goals逻辑的部分。 对于这个问题,我们只关注@unaccomplished_goals 。 在应用程序的侧边栏中正确显示,它来自goals_controller #index @top_3_goals = @unaccomplished_goals.top_3 但是如果我点击链接目标/ new或目标/ 1 /编辑我得到可怕的: undefined method each for nil:NilClass ! 这是一个视图细分: 我在views/layouts/application.html.erb使用来抓取views/sidebar/_sidebar.html.erb ,以便使用来抓住views/goals/_upcoming.html.erb 意见/目标/ _upcoming.html.erb 视图/侧边栏/ _sidebar.html.erb Upcoming 目标控制器 def index if params[:tag] @goals = Goal.tagged_with(params[:tag]) else @goals = Goal.all.order(“deadline”) @accomplished_goals = current_user.goals.accomplished @unaccomplished_goals = current_user.goals.unaccomplished @top_3_goals = @unaccomplished_goals.top_3 end end 目标模型 class Goal { where(accomplished: […]

消息控制器中未定义的方法`stringify_keys’

当我尝试提交表单时出现以下错误(当1个用户想要向另一个用户发送消息时,表单会显示) NoMethodError in MessagesController#create undefined method `stringify_keys’ for “”:String Rails.root: /Users/fkhalid2008/loand Application Trace | Framework Trace | Full Trace app/controllers/messages_controller.rb:31:in `new’ app/controllers/messages_controller.rb:31:in `create’ 以下是消息控制器的相关代码: def new @message = Message.new if params[:reply_to] @reply_to = @user.received_messages.find(params[:reply_to]) unless @reply_to.nil? @message.to = @reply_to.sender.login @message.subject = “Re: #{@reply_to.subject}” @message.body = “\n\n*Original message*\n\n #{@reply_to.body}” end end end def create @message = […]

state_machine gem与rails 4集成

我正在使用我的rails 4应用程序使用state_machine gem时遇到问题。 A有一个模型,其中包含列调用状态,如提供的rails教程http://gistflow.com/posts/679-state-machine-with-rails-basics中所述 但是当我定义我的state_machine时如下: class Issue :new do state :new, value: 0 state :analysed, value: 1 state :assigned, value: 2 state :inprogress, value: 3 state :inreview, value: 4 state :validation, value: 5 state :resolved, value: 6 state :cancelled, value: 7 state :closed, value: 8 state :rejected, value: 9 state :reopened, value: 10 end def […]

CartsController中的NoMethodError#destroy – 使用Rails进行敏捷Web开发4

我正在学习使用Rails 4的敏捷Web开发书之后的Rails,我遇到了这个错误: NoMethodError in CartsController#destroy undefined method `name’ for nil:NilClass 这是Cart控制器的动作Destroy。 @cart.destroy if @cart.id == session[:cart_id] ,错误信息引用@cart.destroy if @cart.id == session[:cart_id]行: # DELETE /carts/1 # DELETE /carts/1.json def destroy @cart.destroy if @cart.id == session[:cart_id] session[:cart_id] = nil respond_to do |format| format.html { redirect_to store_url, notice: ‘Your cart is currently empty’} format.json { head :no_content } […]

Rails 4:Model:Class的未定义方法`relation_delegate_class’

我正在尝试按照这个关于为Rails创建一个Scoped邀请系统的编码器教程。 在我的Rails 4应用程序中,我有以下模型: class User “Invite”, :foreign_key => ‘recipient_id’ has_many :sent_invites, :class_name => “Invite”, :foreign_key => ‘sender_id’ end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through: :administrations has_many :invites end class Administration < ActiveRecord::Base belongs_to :user belongs_to :calendar end class Invite ‘User’ belongs_to :recipient, :class_name => ‘User’ end 以下是我的模型与教程中的模型之间的对应关系: User User Calendar UserGroup Administration […]

Rails + CarrierWave:NoMethodError:nil的未定义方法`name’:NilClass

我正在使用CarrierWave和Rails 3.1。 我在提交表单(尝试上传图片)时收到以下错误消息: 错误信息: ActiveRecord::StatementInvalid in Admin::PostsController#create NoMethodError: undefined method `name’ for nil:NilClass: INSERT INTO “posts” (“body”, “created_at”, “draft”, “image”, “post_type”, “title”, “updated_at”, “user_id”) VALUES (?, ?, ?, ?, ?, ?, ?, ?) Rails.root: /Users/aziz/Sandbox/ruby/rails/Tumblelog Application Trace | Framework Trace | Full Trace app/controllers/admin/posts_controller.rb:18:in `create’ Request Parameters: {“utf8″=>”✓”, “authenticity_token”=>”za+zNRDGNCcujnCmO726cWCo2ze1rgaXv5bL17JGaek=”, “post”=>{“image”=>#<ActionDispatch::Http::UploadedFile:0x000001014aeff0 @original_filename="AzizLight.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; […]

rails try方法抛出NoMethodError?

为什么尝试抛出错误? 这不是打败了整个目的吗? 也许只是在控制台? ruby-1.9.2-p180 :101 > User.first.try(:something) NoMethodError: undefined method `something’ for # from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in `method_missing’ from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.10/lib/active_record/attribute_methods.rb:46:in `method_missing’ from (irb):101 from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start’ from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start’ from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands.rb:23:in `’ from script/rails:6:in `require’ from script/rails:6:in `’ 编辑: 谢谢你们,现在我明白了。 有没有办法在不使用respond_to?情况下做我想做的事情respond_to? ,这样User.try(:something)返回nil而不是抛出错误?

Rails 3.2 undefined方法`key?’ 为零:NilClass

出于某种原因,我在切换到Rails 3.2后开始出现此错误。 我想这与acl9插件有关,我尝试重新安装,但没有任何改变。 我将插件移动到lib / plugins并将初始化程序添加到配置/初始化程序,但同样的错误。 我在Github的acl9 repo上寻找解决方案,但在那里找不到任何东西。 也许它毕竟不是acl9。 我安装了paperclip,acl9,authlogic。 NoMethodError (undefined method `key?’ for nil:NilClass): actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:36:in `visible_action?’ actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:18:in `method_for_action’ actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:14:in `method_for_action’ actionpack (3.2.1) lib/action_controller/metal/compatibility.rb:61:in `method_for_action’ actionpack (3.2.1) lib/abstract_controller/base.rb:115:in `process’ actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process’ actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch’ actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch’ actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action’ actionpack […]

Rake NoMethodError:未定义的方法’scan’for Lexicon:Class

我收到NoMethodError:当我尝试使用以下RakeFile运行rake测试时,Lexicon:Class的未定义方法’scan’ 。 require ‘./lib/ex48/lexicon.rb’ require ‘test/unit’ class TestLexicon < Test::Unit::TestCase def test_directions assert_equal(Lexicon.scan('north'), [%w(direction north)]) end end 我有一个简单的Lexicon类: class Lexicon def initialize @direction = %w(north south east west down up left right back) @verbs = %w(go stop kill eat) @stop_words = %w(the in of from at it) @nouns = %w(door bear princess cabinet) @numbers = […]

使用用户输入的变量NoMethodError访问哈希密钥

我在尝试使用用户输入的变量访问哈希时收到此错误消息:“undefined method`index’for nil:NilClass(NoMethodError)” 这应该说明我的问题好一点……一旦字符串“n”存储在变量中,变量就不能再访问哈希并返回nil。 为什么? [6] pry(main)> line = “n” => “n” [7] pry(main)> subway[:line] => nil [8] pry(main)> subway[:”n”] => [“timessquare”, “34th”, “28th”, “23rd”, “unionsquare”, “8th”] [9] pry(main)> line => “n” This is the .rb file n = [“timessquare”, “34th”,”28th”,”23rd”,”unionsquare”,”8th”] l = [“8th”, “6th”, “unionsquare”,”3rd”,”1st”] six = [“grandcentral”,”33rd”,”28th”,”23rd”,”unionsquare”,”astorplace”] subway = {:n=>n, :l=> l, :six=>six} […]