[英]Rails: Render a .js.erb from another controller?
How can I render a .js.erb
from a controller that doesn't belong to the view it is going to be rendered in? 如何从不属于将要呈现的视图的控制器渲染.js.erb
?
For example: How can I render create.js.erb
in my pages view, from a post_controller
? 例如:如何在post_controller
在我的页面视图中渲染create.js.erb
?
post_controller.rb post_controller.rb
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
format.js #{I think I need something in here?}
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
It seems there are several ways to achieve this: 似乎有几种方法可以实现这一目标:
respond_to do |format|
format.js { :location => path_to_controller_method_url(argument) }
end
How do you respond_to another js file in the controller using Ruby on Rails? 你如何使用Ruby on Rails响应控制器中的另一个js文件?
respond_to do |format|
format.js {
:template => "classrooms/create_differently.js.erb",
:layout => false
}
end
Render alternate view in ruby on rails 在轨道上的红宝石中渲染备用视图
respond_to do |format|
format.js { render :file => "/path/to/save.js.erb" }
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.