我需要阻止respond_to不渲染到new.html.erb,如果不是我创建的另一个视图,例如new_form.html.erb
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
答案 0 :(得分:2)
非常简单。只要视图位于控制器的默认目录中:
respond_to do |format|
format.html render 'new'
format.json { render json: @user }
end
如果没有,您需要告诉它目录:
respond_to do |format|
format.html render 'users/new'
format.json { render json: @user }
end
此处有更多文档:http://guides.rubyonrails.org/layouts_and_rendering.html
答案 1 :(得分:1)
有很多方法可以做到....
##FOR HTML CALLS
format.html { render 'new'}
format.html { render 'shared/new'}
##FOR JS CALLS
format.js { render 'new'}
format.js { render 'shared/new'}
##pass variable to the view
format.js { render 'shared/new',:locals=>{:type=>"User"}}
##OR you can also try redirect in some rare cases WITHOUT respond_to block
redirect_to users_path(params[:id])