NoMethodError(nil的未定义方法`id':NilClass)

时间:2016-04-16 08:44:57

标签: ruby-on-rails redmine redmine-api

当我通过cURL请求在Redmine上创建票证时,我收到此错误。我的cURL正在成功创建票证,但它也返回错误,我无法理解为什么会这样。

这是我得到的错误:

Started POST "/issues.json" for 122.161.50.0 at 2016-04-16 07:18:22 +0000
Processing by IssuesController#create as JSON
Parameters: {"issue"=>{"project_id"=>10, "tracker_id"=>7, "status_id"=>1, "priority_id"=>3, "subject"=>"Issue created from CURL"}}
Current user: himanshu (id=3)
Rendered mailer/_issue.text.erb (6.8ms)
Rendered mailer/issue_add.text.erb within layouts/mailer (45.6ms)
Rendered mailer/_issue.html.erb (2.3ms)
Rendered mailer/issue_add.html.erb within layouts/mailer (6.9ms)
Completed 500 Internal Server Error in 724.8ms

NoMethodError (undefined method `id' for nil:NilClass):
lib/redmine/hook.rb:61:in `block (2 levels) in call_hook'
lib/redmine/hook.rb:61:in `each'
lib/redmine/hook.rb:61:in `block in call_hook'
lib/redmine/hook.rb:58:in `tap'
lib/redmine/hook.rb:58:in `call_hook'
lib/redmine/hook.rb:153:in `call_hook'
app/controllers/issues_controller.rb:151:in `create'

这是控制器代码

def create
call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
@issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
if @issue.save
  call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
  respond_to do |format|
    format.html {
      render_attachment_warning_if_needed(@issue)
      flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
      if params[:continue]
        attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
        redirect_to new_project_issue_path(@issue.project, :issue => attrs)
      else
        redirect_to issue_path(@issue)
      end
    }
    format.api  { render :action => 'show', :status => :created, :location => issue_url(@issue) }
  end
  return
else
  respond_to do |format|
    format.html { render :action => 'new' }
    format.api  { render_validation_errors(@issue) }
  end
end
end

这是调用钩子的代码

def call_hook(hook, context={})
    [].tap do |response|
      hls = hook_listeners(hook)
      if hls.any?
        hls.each {|listener| response << listener.send(hook, context)}
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

@Himanshu Upadhyaya ,你能告诉你 render_attachment_warning_if_needed ,我认为它改变了你的@issue。

哪一个是你控制器中的第151行。我需要知道。

请检查