相关疑难解决方法(0)

Rails - 如何在控制器内使用帮助程序

当我意识到你应该在视图中使用帮助器时,我需要一个帮助器在我的控制器中,因为我正在构建一个JSON对象来返回.

它有点像这样:

def xxxxx

   @comments = Array.new

   @c_comments.each do |comment|
   @comments << {
     :id => comment.id,
     :content => html_format(comment.content)
   }
   end

   render :json => @comments
end
Run Code Online (Sandbox Code Playgroud)

我怎样才能访问我的html_format助手?

ruby-on-rails ruby-on-rails-3 ruby-on-rails-5

198
推荐指数
6
解决办法
16万
查看次数

我在哪里为ActionMailer视图添加帮助方法?

我有一个方法,它接受一个字符串数组并加入它们,所以他们做这样的事情:

>> my_arr
=> ["A", "B", "C"]
>> and_join(my_arr)
=> "A, B, and C"
Run Code Online (Sandbox Code Playgroud)

我希望我的邮件程序可以访问,以便我可以将一些信息输出到电子邮件中.我似乎无法找到一个好的地方把它放在application_helper.rb文件中,它在那里找不到它.它应该去哪里?

ruby-on-rails actionmailer helpermethods

23
推荐指数
5
解决办法
1万
查看次数

Rails 4.1:在 ActionMailer::Preview 中访问 c​​urrent_user

Rails 4.1 有一个很好的方法来预览邮件程序ActionMailer::Preview。我的所有邮件程序都带有一个user参数,我想current_user(从 Devise)传入该参数以进行预览。

如果我尝试这个,它不起作用。

class SubscriptionsMailerPreview < ActionMailer::Preview
  # Preview this email at http://localhost:3000/rails/mailers/subscriptions_mailer/new
  def new
    SubscriptionsMailer.new(current_user)
  end
end
Run Code Online (Sandbox Code Playgroud)

它返回undefined local variable or method 'current_user' for #<SubscriptionsMailerPreview:0xa6d4ee4>

我怀疑这是因为current_user由 Devise 在 中定义ApplicationController,并且根据文档,ActionMailer使用AbstractController::Base. 在这种情况下,存储current_user在类变量中是一个坏主意吗?

有谁知道我如何使用current_user帮助程序ActionMailer::Preview

ruby-on-rails actionmailer ruby-on-rails-4.1

6
推荐指数
1
解决办法
868
查看次数

如何访问rails动作邮件程序中的current_user

嗨,我必须访问我的动作邮件程序中的当前用户,但我收到以下错误

undefined local variable or method `current_user' for #<WelcomeMailer:0xa9e6b230>
Run Code Online (Sandbox Code Playgroud)

通过使用此链接,我使用应用程序帮助程序来获取当前用户.这是我的WelcomeMailer

class WelcomeMailer < ActionMailer::Base
  layout 'mail_layout'

  def send_welcome_email
    usr = find_current_logged_in_user
    Rails.logger.info("GET_CURRENT_USER_FROM_Helper-->#{usr}")
  end
end
Run Code Online (Sandbox Code Playgroud)

我的应用程序助手如下

def find_current_logged_in_user
    #@current_user ||= User.find_by_remember_token(cookies[:remember_token])
    # @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id])
    Rails.logger.info("current_user---> #{current_user}")
    current_user
  end
Run Code Online (Sandbox Code Playgroud)

我也尝试过session和cookies.那么我该如何解决这个错误,或者是否有其他方法来访问动作邮件程序中的当前用户.我在用Rails 3.2.14 and ruby ruby 2.1.0

ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

6
推荐指数
1
解决办法
3943
查看次数

Rails 4:如何在邮件视图中使用ApplicationHelper方法

我在application_helper.rb中定义了一个助手,希望在邮件视图中使用它,但是只是在做

<%= helper_method(param) %>
Run Code Online (Sandbox Code Playgroud)

在mailer_view.html.erb文件中,显示“未定义方法”错误。

还有另一种方法吗?我讨厌不得不将同一个助手放在其他地方。

谢谢。

actionmailer helpers ruby-on-rails-4

3
推荐指数
1
解决办法
893
查看次数