首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在图像不可用时使用refile gem时捕获异常

在图像不可用时使用refile gem时捕获异常
EN

Stack Overflow用户
提问于 2015-01-22 09:46:13
回答 1查看 493关注 0票数 0

在我的控制台里面,它显示了username@email.com.attacher_。我非常确定重文件不起作用,因为用户的图像不在那里。但是,当file为nil时,我该如何设置它来处理方法块呢?

代码语言:javascript
复制
  module MessagesHelper

      def recipients_options
        s = ''
        users = User.all + BizUser.all; users.each do |user|
          s << "<option value='#{user.id}'
    data-img-src='#{attachment_url(@user, user.email, :profile_avatar,
    :fill, 50, 50)}'>#{user.username}</option>"
        end
        s.html_safe
      end
    end

下面是我的日志文件中显示的错误

代码语言:javascript
复制
NoMethodError in Messages#new
ActionView::Template::Error (undefined method `profile_avatar_attacher' for nil:NilClass):
     9:   <div class="row">
    10:     <div class="large-5 medium-5 small-4 columns" style="padding-bottom: 10px;">
    11:       <%= label_tag 'recipients', 'Choose recipients' %>
    12:       <%= select_tag 'recipients', recipients_options, multiple: true, class: 'form-control chosen-it' %>
    13:     </div>
    14:   </div>
    15:   <div class="row">
  app/helpers/messages_helper.rb:6:in `block in recipients_options'


module MessagesHelper

  def recipients_options
    s = ''
    users = User.all + BizUser.all; users.each do |user|
      s << "<option value='#{user.id}' data-img-src='#{attachment_url(@user, :profile_avatar, :fill, 30, 30)}'>#{user.username}</option>"
    end
    s.html_safe
  end
end

用于头像方法的ApplicationHelper

代码语言:javascript
复制
def avatar_for_user
    image_tag attachment_url(@user, :profile_avatar, :fill, 30, 30)
  end

我在participants.html.erb部分中也收到了一个错误

代码语言:javascript
复制
<% conversation.participants.each do |participant| %>
    <% unless participant == current_user %>
        <%= avatar_for_user participant %>
    <% end %>
<% end %>
EN

回答 1

Stack Overflow用户

发布于 2015-01-26 20:45:39

您混淆了user局部变量和@user实例变量。

recipients_options中,可以调用

代码语言:javascript
复制
attachment_url(@user :profile_avatar, :fill, 30, 30)

您为每个选项呈现相同的附件(用于@user的附件)。

我怀疑你是想打电话给

代码语言:javascript
复制
attachment_url(user, :profile_avatar, :fill, 30, 30)

从而呈现每个用户的附件。

出现NoMethod-error的原因似乎是@user变量为空。

而且,avatar_for_user看起来很奇怪。您可以不带参数地定义它,但在部分中将它传递给用户。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28080057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档