如何.js.erb从不属于将要渲染到的视图的控制器中渲染?
.js.erb
例如: 如何create.js.erb从页面渲染页面视图post_controller?
create.js.erb
post_controller
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
似乎有几种方法可以实现此目的:
respond_to do |format| format.js { :location => path_to_controller_method_url(argument) } end
您如何使用Ruby onRails响应控制器中的另一个js文件?
respond_to do |format| format.js { :template => "classrooms/create_differently.js.erb", :layout => false } end
在Rails中渲染红宝石的替代视图
respond_to do |format| format.js { render :file => "/path/to/save.js.erb" } end