【问题标题】:Rails: Routing error when using ajax with a buttonRails:使用带有按钮的ajax时出现路由错误
【发布时间】:2019-03-31 02:40:42
【问题描述】:

我是 Rails 新手,并已关注此 question 上的答案。

这就是我的项目中的情况:

控制器:

def create
  def create
      current_user.likes.create(:post_id => params[:post_id])
      render :layout => false
   end
end

.js 文件:

$ ->
    $(".like").click ->
        post_id = $(this).attr('id')
        $.ajax
            type: 'POST'
            url: 'likes/' + post_id
            success: ->
                alert "succsess!"

Routes.rb

Sample::Application.routes.draw do
  resources :likes

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :posts, only: [:create, :destroy]

  root              to: 'pages#home'
  match '/about',   to: 'pages#about'
  match '/contact', to: 'pages#contact'
  match '/help',    to: 'pages#help'
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy'
  post  '/likes/4', to: 'likes#create', :as => :like
end

(我使用 '/likes/4' 来测试提议(将来会是 'post_id')。

当我点击视图中的 Like 按钮时,like 会存储在数据库中,但我收到此错误(当我在 Chrome 中使用检查器查看控制台时)...

POST http://0.0.0.0:3000/likes/4 500 (Internal Server Error)

...我从来没有从 ajax 收到成功警报。

当我运行wget --post-data='' http://localhost:3000/likes/4 时,我得到:

--2012-07-22 19:35:01--  http://localhost:3000/likes/4
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:3000... failed: Connection refused.
Connecting to localhost|127.0.0.1|:3000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2012-07-22 19:35:02 ERROR 500: Internal Server Error.

有人知道是什么导致了这个错误吗?

【问题讨论】:

    标签: ruby-on-rails ajax


    【解决方案1】:

    我认为这是因为您发布了一个帖子,并且您的路线是一条捷径。您可以在录制命令rake routes 时显示它。尝试将您的 ajax 请求类型更改为 GET 或更改您的路线。将match 替换为post

    【讨论】:

    • 谢谢豆鬼!当我在 .js 文件中将 'GET' 替换为 'POST' 时,它可以工作,但我需要使用 ajax 请求发送一些信息(然后我需要使用 POST,对吗?)。但是当我在 routes.rb 中将 'match' 替换为 'post' 时它不起作用。有什么建议吗?
    • 我的意思是将 .js 文件中的 'POST' 替换为 'GET' :)
    • 我认为您也可以使用 GET 发送一些信息。请发送rake routes 命令的结果。
    • 这是我在 .js 文件中使用 'GET' 而在 routes.rb 文件中使用 'match' 的时候。这是你需要的吗? (对不起所有的 cmets,但很难让它看起来可读)。
    • likes_4 /likes/4(.:format) likes#create 不正常。你可以发送你的 routes.rb (编辑你的第一篇文章)。我理解所有的cmets。
    【解决方案2】:

    好的,我找到了问题!

    首先,感谢斗鬼提出的一些有益的观点,帮助了我!

    解决方案:

    我不得不从以下位置更改唯一性验证:

    validates :tag, :uniqueness => {:scope => :post}
    

    到:

    validates :tag_id, :uniqueness => {:scope => :post_id}
    

    你可能想知道为什么?这里解释一下:http://thetenelements.blogspot.no/2011/08/undefined-method-text-for-nilnilclass.html

    所以这是我工作时的文件:

    like.rb

      validates :user_id, :uniqueness => {:scope => :post_id}
      belongs_to :user
      belongs_to :post
    

    likes_controller.rb

    def userLikePost
       current_user.likes.create(:post_id => params[:post_id])
    end
    

    routes.rb

    match '/likes/:post_id', to: 'likes#userLikePost'
    

    pages.js.coffee

    $ ->
        $(".like.btn.btn-mini").click (e) ->
            if $(this).attr("class") == "like btn btn-mini"
                post_id = $(this).attr('id')
                $.post '/likes/' + post_id
            e.stopImmediatePropagation();
            $(this).addClass("active")
    

    html 按钮

    <button class="like btn btn-mini" id="<%= feed_item.id %>"><i class="icon-heart"></i></button>
    

    在用户和帖子模型中我有has_many: likes

    希望这可以帮助其他人:)

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2014-02-13
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多