Lotus@ 2017-06-07 01:17 采纳率: 100%
浏览 5

用ajax更改rails值

I am trying to change a rails value with ajax and I am having trouble. Here is what I have so far.

ajax

$(button).click(function(){
        $.ajax({
           url: '/change_value',
           type: 'GET'
    })
});

Controller

def home
  @value = 0
end

def change_value
  @value = 1
end

routes

get '/change_value', to: 'static_pages#change_value'

Thank you for all the help.

  • 写回答

1条回答 默认 最新

  • weixin_33716941 2017-06-07 01:23
    关注

    You need to create a view to respond in js format:

    change_value.js.erb

    var newValue = <%= @value %>;
    

    Now, use it in your script:

    $(button).click(function(){
            $.ajax({
               url: '/change_value',
               type: 'GET',
               success: function(data) {
                   console.log(newValue);
               }
        })
    });
    
    评论

报告相同问题?