简体   繁体   English

jQuery在Michael Hartl教程的第11章中无法执行删除操作

[英]jQuery not working at delete action on Michael Hartl's tutorial, chapter 11

I'm working on rails tutorial by Michael Hartl like other newbies in rails do. 我正在像其他新手一样,在Michael Hartl编写的Rails教程上工作。 anyway, I'm stuck at the chapter 11 like those too with some JQuery not working with delete action issue. 无论如何,我也像其他人一样被第11章所困扰,有些JQuery不能解决删除动作问题。 That's to say, when clicking on any delete button, it only redirects me to a particular error page saying "No route matches [GET] "/microposts/301" 也就是说,当点击任何删除按钮时,它只会将我重定向到一个特定的错误页面,提示“没有匹配[GET]的路由” // microposts / 301”

My code in the _micropost.html.erb page is as below: 我在_micropost.html.erb页面中的代码如下:

<tr>
  <td class="micropost">
    <span class="content"><%= micropost.content %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(micropost.created_at) %> ago.
    </span>
  </td>
  <% if current_user?(micropost.user) %>
  <td>
    <%= link_to "delete", micropost, :method => :delete,
                                     :confirm => "You sure?",
                                     :title => micropost.content %>
  </td>
  <% end %>
</tr>

So, I checked with my gem file whether it had prototype and jQuery conflict or not. 因此,我检查了我的gem文件是否具有原型和jQuery冲突。 what I found is as below 我发现如下

source 'http://rubygems.org'

gem 'rails', '3.1.1'
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'

gem 'gravatar_image_tag', '1.0.0.pre2'
gem 'will_paginate', '3.0.pre2'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  #gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'


group :development do
  gem 'rspec-rails', '2.6.1'
  gem 'faker', '0.3.1'
end

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'factory_girl_rails', '1.0'
end

group :production do
  gem 'therubyracer-heroku', '~> 0.8.1.pre3', :platform => :ruby
  #gem 'therubyracer-heroku', '0.8.1.pre3' # you will need this too
  gem 'pg'
end

So, I am quite so sure here there is no prototype installed, as I heard for the rails 3.1 onward the jQuery is used by default. 因此,我非常确定这里没有安装原型,因为我听说在Rails 3.1以后的版本中默认使用jQuery。

Any advice that you think to get it working for me? 您认为有什么建议对我有用吗? Where to check that my jQuery is already installed properly. 在哪里检查我的jQuery是否已正确安装。

As per @Tuck's suggestion, I post my routes.rb here. 按照@Tuck的建议,我在这里发布我的routes.rb。

SampleApp::Application.routes.draw do
  resources :users
  resources :sessions, :only => [:new, :create, :destroy]
  resources :microposts, :only => [:create, :destroy]

  match '/signup',  :to => 'users#new'
  match '/signin',  :to => 'sessions#new'
  match '/signout', :to => 'sessions#destroy'

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'

  root :to => 'pages#home'

  get "pages/home"

  get "pages/contact"

  get "pages/about"

end

While I've never used RoR, it seems like you're having a classic issue faced by any developer leveraging an MVC framework - that is, the action in yoru link_to handler points to an invalid route. 尽管我从未使用过RoR,但是似乎任何开发人员都在利用MVC框架时遇到了一个经典问题-也就是说,yoru link_to处理程序中的操作指向无效的路由。 Based on your code it's apparent that micropost is your model, but we have no idea where that model is being sent... Can you a) take a look at the html output from the script (use your browser's built-in developer tools or just view source) and verify that the generated url is valid and/or b) post the route mappings that are configured for your app? 根据您的代码,很明显,micropost是您的模型,但我们不知道该模型将发送到哪里...您能否a)查看脚本的html输出(使用浏览器的内置开发人员工具,或者只需查看源代码)并验证生成的网址是否有效和/或b)发布为您的应用配置的路由映射? Posting either/or would make it much easier for the community to help you troubleshoot the problem... 发贴/或发贴将使社区更轻松地帮助您解决问题...

Check if you see the line <%= stylesheet_link_tag %> in your layout file application.html.erb 检查在布局文件application.html.erb中是否看到<%= stylesheet_link_tag%>行

If you don't have it then your delete script wouldn't work 如果您没有它,那么您的删除脚本将无法工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM