【问题标题】:Reloading only Div using Ajax with rails使用带有rails的Ajax仅重新加载Div
【发布时间】:2014-05-18 10:58:43
【问题描述】:

通过我的表 (id = "portfolios"),点击一个 cel,我将值发送到我的 rails 控制器 /game/search。

此查询工作正常,但整个页面正在重新加载。我只想重新加载表。有什么办法吗?

function getval(cel) {


    if (isNaN(cel)==true){

          window.alert("Oops! Click again.");

        }
        else {
          c="<% cut_line %>";


jQuery.ajax({
            data: 'q=' + cel,
            //dataType: 'script',
            type: 'post',
            url: "/game/search"      
          });
          //post_to_url("/game/search", {q: cel});

        }
    }

这是控制器动作:

def search
Game.new.update_matrix(params[:q], Ip.where(ip: request.remote_ip).last)
redirect_to :action => :play
end

【问题讨论】:

  • 你能粘贴你的控制器动作吗?

标签: jquery html ruby-on-rails ajax refresh


【解决方案1】:

要解决您的问题,您需要添加到控制器操作中

respond_to do |format|
  format.html
  format.json {render json: object}
end

您传递给 ajax 的 url 应该具有 .json 扩展名,或者如果您不想要,请在路由文件中将资源设置为 defaults: {format: 'json'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多