0
votes

I'm working on rails tutorial by Michael Hartl like other newbies in rails do. anyway, I'm stuck at the chapter 11 like those too with some JQuery not working with delete action issue. 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"

My code in the _micropost.html.erb page is as below:

<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. 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.

Any advice that you think to get it working for me? Where to check that my jQuery is already installed properly.

As per @Tuck's suggestion, I post my routes.rb here.

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
2
You can check if jQuery is loading by checking in Firebug/Chrome in the "scripts" section. But it's more likely it's an issue with routing or the controller.Dave Newton

2 Answers

1
votes

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. 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? Posting either/or would make it much easier for the community to help you troubleshoot the problem...

0
votes

Check if you see the line <%= stylesheet_link_tag %> in your layout file application.html.erb

If you don't have it then your delete script wouldn't work