我正在尝试生成包含一些HTML的JSON响应.因此,我有/app/views/foo/bar.json.erb:
{
someKey: 'some value',
someHTML: "<%= h render(:partial => '/foo/baz') -%>"
}
Run Code Online (Sandbox Code Playgroud)
我希望它渲染/app/views/foo/_baz.html.erb,但它只会渲染/app/views/foo/_baz.json.erb.传球:format => 'html'没有帮助.
我在Rails 4.0上.
我正在发送这样的事件(注意:remote => true):
<%= button_to 'yes', {controller:'videos', action:'rate', id: video.hashed_id, yesno:'yes'}, {:remote=>true, :class=>"rate-btn yes-btn btn btn-default btn-sm"} %>
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像这样:
def rate
video = Video.find_by( hashed_id: params[:id])
action = params[:yesno]
puts video.hashed_id
puts action
respond_to do |format|
if (action=='yes')
new_rating = video.rating==1 ? 0 : 1
video.update( is_new:0, rating: new_rating )
format.html { redirect_to controller:'projects', action: show, id: video.project.hashed_id }
format.json { head :no_content }
format.js { render :nothing=>true }
end
if (action=='no')
new_rating = video.rating==-1 ? 0 : …Run Code Online (Sandbox Code Playgroud)