我正在尝试选择youtube视频应该依赖于具有即时变量的操作的高度和宽度。但它不起作用。
我的控制器:
class PublicController < ApplicationController
def index
@posts = Post.order('created_at DESC').page(params[:page]).per(5)
@width = '600'
@height = '600'
end
end
我的模特:
def body_html
auto_html self[:body_html] do
html_escape
image
youtube(:width => @width, :height => @height)
link :target => "_blank", :rel => "nofollow"
simple_format
end
end
答案 0 :(得分:1)
我认为body_html是你模型的方法
您在控制器的上下文中设置实例变量@width和@height,而不是模型
试试这个:
class PublicController < ApplicationController
def index
@posts = Post.order('created_at DESC').page(params[:page]).per(5)
@posts.each do |post|
post.width = '600'
post.height = '600'
end
end
end
P.S。
你的问题实际上是对MVC模式的错误理解,你的body_html方法过于表现,应该被提取到单独的_post.html.erb模板或其他
该模板可以访问控制器中定义的所有实例变量,因此原始控制器方法可以保持不变
P.P.S。
这里有一些快速代码片段:
控制器中的:
def index
@posts = Post.all
@width = @height = '600'
end
index.html.erb中的
<%= render :collection => @posts %>
_post.html.erb 中的
...
<%= post_youtube(post, @width, @height) %>
...