我希望我用ActionMailer发送的电子邮件包含链接回我的应用程序的图像.在我的开发环境中,例如:
<img src="http://myfullappurl.dev/assets/myimage.png">
Run Code Online (Sandbox Code Playgroud)
我在我的开发中有这个.rb
config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = { host: 'myfullappurl.dev', only_path: false }
Run Code Online (Sandbox Code Playgroud)
但我不能让我的邮件模板以这些方式呈现完整的URL:
asset_path('myimage.png')
asset_path('myimage.png', only_path: false)
image_url('myimage.png')
Run Code Online (Sandbox Code Playgroud)
通过这样的方式回答了很多关于这个主题的类似问题:
"#{request.protocol}#{request.host_with_port}#{asset_path('image.png')}"
Run Code Online (Sandbox Code Playgroud)
但是因为我的邮件是与Sidekiq异步发送的,所以没有任何request对象,如果它们是在控制器中同步发送的话.
有没有明显的东西我想念,或者我是否必须与Rails作斗争才能做到这一点?我正在使用Rails 4,但我确信在3.1/3.2中有效的任何东西都可以.
假设我有一个Person类和一个Gang类
class Person
belongs_to :gang
attr_accessible :name, :secret
def to_builder
Jbuilder.new do |app|
person.id id
person.name name
end
end
end
class Gang
has_many :people
attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)
如何从视图中使用此to_builder方法?
例如
#app/views/gang/show.json.jbuilder (@gang set by the controller)
json.gang do |json|
json.name @gang.name
json.gang_members(@gang.people) do |person|
#how do I delegate to the person.to_builder here?
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,我不想只使用默认的Person.as_json,因为我不想secret在Person上呈现属性.
我尝试过的大多数事情最终都是渲染了Person.as_json,而不是Person.to_builder.
我希望能够在模块中使用包含该模块的类无法访问的方法.给出以下示例:
class Foo
include Bar
def do_stuff
common_method_name
end
end
module Bar
def do_stuff
common_method_name
end
private
def common_method_name
#blah blah
end
end
Run Code Online (Sandbox Code Playgroud)
我希望Foo.new.do_stuff爆炸,因为它试图访问模块试图隐藏它的方法.但是,在上面的代码中,Foo.new.do_stuff可以正常工作:(
有没有办法在Ruby中实现我想做的事情?
更新 - 真正的代码
class Place < ActiveRecord::Base
include RecursiveTreeQueries
belongs_to :parent, {:class_name => "Place"}
has_many :children, {:class_name => 'Place', :foreign_key => "parent_id"}
end
module RecursiveTreeQueries
def self_and_descendants
model_table = self.class.arel_table
temp_table = Arel::Table.new :temp
r = Arel::SelectManager.new(self.class.arel_engine).from(model_table).project(model_table.columns).join(temp_table).on('true').where(model_table[:parent_id].eq(temp_table[:id]))
nr = Place.scoped.where(:id => id)
q = Arel::SelectManager.new(self.class.arel_engine)
as = Arel::Nodes::As.new temp_table, nr.union(r)
arel = Arel::SelectManager.new(self.class.arel_engine).with(:recursive,as).from(temp_table).project(temp_table[:id])
self.class.where(model_table[:id].in(arel))
end …Run Code Online (Sandbox Code Playgroud) 假设我在没有使用强参数的控制器中有代码
Model.create name: params[:name], alias_id: params[:foreign_id]
Run Code Online (Sandbox Code Playgroud)
我可以在这里使用强参数吗?
我不能做
Model.create params.require(:name, :foreign_id)
Run Code Online (Sandbox Code Playgroud)
因为foreign_id不是一个参数
我不能做
Model.create params.require(:name, :alias_id)
Run Code Online (Sandbox Code Playgroud)
因为alias_id不在模型上.
基本上,我想知道在使用强参数时是否可以使用参数键别名.
ruby-on-rails ruby-on-rails-3 strong-parameters ruby-on-rails-4