在AJAX中我写道:
$.ajax({
URL: '/new_avatar',
type: 'POST',
data: { username: 'alizade' },
success: function(response){
alert(response);
},
})
Route.rb:
post '/new_avatar' => 'avatars#new_avatar'
Avatar.rb型号:
self.new_avatar(username)
Avatar.where(username: username).select('avatar').last
end
Avatars_Controller:
def new_avatar
@username = params[:username]
@result = Avatar.new_avatar(@username)
end
那么,如何将@result发送到AJAX响应函数并提醒数据库选择结果?
答案 0 :(得分:0)
您需要在控制器方法中使用render :text
def new_avatar
@username = params[:username]
@result = Avatar.new_avatar(@username)
render :text => "#{@username} has this result:- #{@result.inspect}"
end
答案 1 :(得分:0)
假设您只需要此服务的响应,您可以使用以下方式获取结果。
def new_avatar
@username = params[:username]
render :json => Avatar.new_avatar(@username)
end