我有一些(或所有)控制器需要的Ruby方法.我试过把它们放进去/app/helpers/application_helper.rb
.我已经将它用于视图中使用的方法.但是控制器看不到那些方法.是否有其他地方我应该放他们或我需要以不同方式访问这些帮助方法?
使用最新的稳定Rails.
ruby ruby-on-rails view-helpers ruby-on-rails-3 ruby-on-rails-3.2
我试图从rails 3邮件程序访问帮助程序方法,以便访问当前用户的会话.
我把帮助器:应用程序放在我的邮件程序类中,这似乎有效,除了我的邮件程序无法使用其中定义的方法(我得到未定义的错误).有谁知道这应该如何工作?
这是我的班级:
class CommentMailer < ActionMailer::Base
default :from => "Andre Fournier <andre@gfournier.com>"
helper :application
end
Run Code Online (Sandbox Code Playgroud)
谢谢,肖恩
我的application_helper.rb文件包含以下代码
module ApplicationHelper
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个有一个方法的帖子控制器
def create
@post = Post.new(params[:post])
@post.posted_by_uid=current_user.id
@post.posted_by=current_user.first_name+" "+current_user.last_name
@post.ups=0
@post.downs=0
@post.save
respond_to do |format|
if @post.save
format.html {redirect_to root_path}
format.json {render :json => @post, :status => :created, :location => @post}
else
format.html {redirect_to root_path}
format.json {render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过以下视图提交表单时
<%= form_for(@post) do |f| %>
<div class="fields">
<%= f.label "Post" %><br />
<%= f.text_field :post %>
</div>
<div class="actions" id="refreshposts"> …
Run Code Online (Sandbox Code Playgroud)