我认为您可能正在寻找一个 respond_to 块以及前面提到的过滤器之前提到的块:
class CompaniesController < ApplicationController
before_filter :login
def new
@company = Company.new
render json: @company, layout: false # layout false if you do not want rails to render a page of json
end
private
def login
if !user_signed_in?
#your login code here
end
end
end
不要忘记将 dataType ajax 选项设置为 json:
$.ajax({
url: "company/new" //your rails url, what is there is just my best guess
data: query, //whatever paramaters you have
dataType: "json", // or html, whichever you desire
type: "GET",
success: function (data) {
//your code here
}
});