ruby-on-rails Redmine + Ruby错误:ActionView::Template::错误-未定义局部变量或方法“块”

2fjabf4q  于 2023-02-10  发布在  Ruby
关注(0)|答案(1)|浏览(241)

在Redmine中,"我的页面"显示了一些内容,包括_issues.erb页面:

<div class="contextual">
  <%= link_to_function l(:label_options), "$('##{block}-settings').toggle();", :class => 'icon-only icon-settings', :title => l(:label_options) %>
</div>

但是在最近更新了一些插件之后,它不再接受此代码,并且在日志中我们看到

ActionView::Template::Error (undefined local variable or method `block' for #<#<Class:0x00007fd303e3a430>:0x00007fd2fc2b3e38>
Did you mean?  @blocks):
    1: <div class="contextual">
    2:   <%= link_to_function l(:label_options), "$('##{block}-settings').toggle();", :class => 'icon-only icon-settings', :title => l(:label_options) %>
    3: </div>
    4: 
    5: <h3>

在同一模板的其他地方,我们看到block只与一个#一起使用,这是正确的吗?例如,不确定这些行是否也有错误,但它们显示了###block的不同使用:

<div id="<%= block %>-settings" style="display:none;">
      <%= form_tag(my_page_path, :remote => true) do %>
        <div class="box">
          <%= render_query_columns_selection(query, :name => "settings[#{block}][columns]") %>
        </div>
        <p>
          <%= submit_tag l(:button_save) %>
          <%= link_to_function l(:button_cancel), "$('##{block}-settings').toggle();" %>
        </p>
      <% end %>
    </div>

这是怎么回事,为什么现在会失败?插件没有对这些页面做任何事情,它们从升级前就没有改变过。
以下是版本信息,如果有帮助的话:

Environment:
  Redmine version                3.4.6.stable
  Ruby version                   2.5.1-p57 (2018-03-29) [x86_64-linux-gnu]
  Rails version                  4.2.11.3
  Environment                    production
  Database adapter               PostgreSQL

编辑:
下面是部分调用堆栈和相关代码片段:

app/views/my/blocks/_issues.erb:2:in `_app_views_my_blocks__issues_erb___4510516419606634452_70176136873380'
  app/helpers/my_helper.rb:133:in `render_issuequery_block'
  app/helpers/my_helper.rb:64:in `render_block_content'
  app/helpers/my_helper.rb:35:in `render_block'
  plugins/redmine_reporter/lib/redmine_reporter/patches/my_helper_patch.rb:19:in `render_block_with_reporter'
  ....

/my_helper_patch.rb:19:in 'render_block_with_reporter'

def render_block_with_reporter(block, user)
          return '' if %w(report_by_issues report_by_spent_time).include?(block) && !user.allowed_to?(:view_time_entries, nil, global: true)

          render_block_without_reporter(block, user)  // Line 19
        end

app/helpers/my_helper.rb:35:in 'render_block'

# Renders a single block
  def render_block(block, user) 
    content = render_block_content(block, user) // Line 35
    if content.present?
      handle = content_tag('span', '', :class => 'sort-handle', :title => l(:button_move))
      close = link_to(l(:button_delete),
                      {:action => "remove_block", :block => block},
                      :remote => true, :method => 'post',
                      :class => "icon-only icon-close", :title => l(:button_delete))
      content = content_tag('div', handle + close, :class => 'contextual') + content

      content_tag('div', content, :class => "mypage-box", :id => "block-#{block}")
    end
  end

app/helpers/my_helper.rb:64:in 'render_block_content'

# Renders a single block content
  def render_block_content(block, user)
    unless block_definition = Redmine::MyPage.find_block(block)
      Rails.logger.warn("Unknown block \"#{block}\" found in #{user.login} (id=#{user.id}) preferences")
      return
    end

    settings = user.pref.my_page_settings(block)
    if partial = block_definition[:partial]
      begin
        render(:partial => partial, :locals => {:user => user, :settings => settings, :block => block})
      rescue ActionView::MissingTemplate
        Rails.logger.warn("Partial \"#{partial}\" missing for block \"#{block}\" found in #{user.login} (id=#{user.id}) preferences")
        return nil
      end
    else
      send "render_#{block_definition[:name]}_block", block, settings   // line 64
    end
  end

app/helpers/my_helper.rb:133:in 'render_issuequery_block'

def render_issuequery_block(block, settings)
    query = IssueQuery.visible.find_by_id(settings[:query_id])

    if query
      query.column_names = settings[:columns] if settings[:columns].present?
      query.sort_criteria = settings[:sort] if settings[:sort].present?
      issues = query.issues(:limit => 10)   // Line 132
      render :partial => 'my/blocks/issues', :locals => {:query => query, :issues => issues, :block => block, :settings => settings}
    else
      queries = IssueQuery.visible.sorted
      render :partial => 'my/blocks/issue_query_selection', :locals => {:queries => queries, :block => block, :settings => settings}
    end
  end

(为了清楚起见,我添加了//行号的"错误"注解)。

flmtquvp

flmtquvp1#

因此,我们通过将最后两个调用堆栈帧中的“block”变量重命名为“something”来“修复”这个问题。
例如

<div class="contextual">
  <%= link_to_function l(:label_options), "$('##{something}-settings').toggle();", :class => 'icon-only icon-settings', :title => l(:label_options) %>
</div>

以及

render :partial => 'my/blocks/issues', :locals => {:query => query, :issues => issues, :block => block, :something => block}

仍然不知道到底是什么原因造成的,但现在它不再是一个问题,这就足够了。

相关问题