Rails4 method: :delete mistakenly routes to [GET] - ruby-on-rails

I am working a basic rails miniblog tutorial that is based on an older rails (dunno if that is the problem). I am trying to implement user logout (terminate a user session).
I have this in my form:
<%= link_to 'Logout', session_path(current_user), method: :delete %>
and my rake routes table looks good:
session DELETE /sessions/:id(.:format) sessions#destroy
but when I test it on local server, by clicking on 'Logout', I was given this error:
No route matches [GET] "/sessions/1"
It really should not have been a [GET] request. I checked that I included all the necessary javascript lines and bundle. What went wrong?
p.s. here is my repo: https://github.com/lukexuanliu/CornellBlog
p.p.s. my problem is very similar to this one: Routing Error No route matches [GET] "/microposts/304 - Deleting a Micropost - Michael Hartl's railstutorial.org Chapter 11
but their solution does not work for me. also, my bootstrap css is kind of messed up... dunno if that contributed to the problem

I think I found the problem. When I said "by clicking on Logout," I actually pull out the html code (using inspect element) to click on the actual code "session/1" which will mistakenly issue a [GET] request. as I clean up my stylesheets (turns out to be incompatibility of bootstrap2 code running on bootstrap3 api) and revealed the Logout button to click on, it works fine. curious.

Related

Routing Error: 2 routes are triggered when link is clicked, yet on client side it works

I cannot find out why when that following route:
get "/instagram", to: "posts#instagram", as: "instagram"
is requested by this code:
<%= link_to "Instagram", instagram_path %>
and perfectly render:
posts/instagram.html.erb
via the PostsController and following action:
def instagram
end
it does render posts/instagram.html.erb but it also try to get to this weird URL GET "/..." that I did not set up in my routes.rb or any where in the rails app.
Everything works fine on the client side and if not looking at the logs I may never have noticed this, but it bother me that this error is triggered: ActionController::RoutingError (No route matches [GET] "/...") as it does not make any sense to me.
It is possible that any ajax function is called when you click on the link, first you should check jquery is not causing the issue.
Second you should check by disabling turbolink, for particular link you can data-no-turbolink to disable turbolink.
Also try in different browser.

Rails POST throws GET error

I have a rails link that's using the POST method:
link_to "Run", forecast_run_path(#forecast), method: :post
to call a defined post route:
resources :forecasts do
post :run
end
The route appears in rake routes output as expected:
forecast_run POST /forecasts/:forecast_id/run(.:format) forecasts#run
And when I inspect the page, the expected post link appears in the page:
Run Forecast
In dev it works fine, and was good in production until sometime just a few days ago - I can't find any change that seems like it should have broken it. But when I click the link, I get the error ActionController::RoutingError (No route matches [GET] "/forecasts/20/run")
I agree that no GET route matches, but it should be using POST.
This is Ruby 2.1.5, Rails 4.2.0
To my own embarrassment and for the benefit of anyone else who has the same issue:
As I switched among different clients, I didn't notice that on one of them NoScript in Firefox was blocking javascript on that site. Doh!
When you post a run, the form will direct you to the show action where it displays the newly posted run, in your case you don't have a show action for run, so implement a show action and should work fine.

Engine's routes not available in host's views

I got an application that I turned into an engine which has tons of routes. I added the engine to a host app via Gemfile and when I run rake routes everything is displayed properly. However, in the views, the routes aren't found resulting in this error:
I access / and get following error:
No route matches {:controller=>"reports" :action=>"new"}
in file /Users/username/Sites/engine_app/app/views/home/index.html.erb:
<%= link_to 'New Report', new_report_path %>
rake routes:
...
reports#new new_report GET /:locale/reports/new(.:format)
...
Also, I can access /en/reports/new which loads the correct controller and view but get another routing error.
To me, it seems I can access any route directly but the app cannot resolve any xxx_path inside the views at all. Any suggestions?
Considering the lack of details in question, I can think only that you are trying to access it from a wrong url. /:locale/reports/new(.:format)
you probably open the url without locale:
localhos:3000/reports/new
but it should be:
localhos:3000/en/reports/new

ActionController::RoutingError (No route matches [GET] PaperClip Error in Ruby on Rails

Paperclip is working fine on my localhost but when I am trying to upload images on my server, I am getting this error (as shown in my production.rb log file)
ActionController::RoutingError (No route matches [GET]
I am very surprised why this is happening. I have not put any :url or :path in any model since anyway it is working fine on localhost without it (I see some of the other answers suggesting that but I am not sure this is the problem). Any one with suggestions on how to fix this paperclip problem ?
EDIT - Ok, while creating a page which has attachment and when I am uploading image, the image is not uploading, I am getting the error above and the image of the path is http://example.com/system/photos/preview_images//original/home.png?1369929849, see the two //, why is this so ? there should be 000/000/004 or something like this there, right ?
If your server is using ssl, you might have to use url instead of path. It works fine in localhost because your localhost will not be using ssl.
For example
=link_to "something", some_path
should be replaced with
=link_to "something", some_url

Rails path nomenclature not working

I have a Fixer model, but, unlike all the other models in my app, its routing isn't working right, even though there's a resource line for it in the routes file.
The problem is, when I try to link to the basic show path any number of ways, like
<%= link_to "Fixer", fixer_path(#fixer) %>
or
<%= link_to "Fixer", #fixer %>
or
<%= link_to "Fixer", fixer_path(#fixer.id) %> # I got desperate
it links to /fixers.[:id] (not a real page) instead of /fixers/[:id]. No idea what's happening, cause my resources line is there, and show is a basic resources action, and all the other similarly resourced models seem to be working just fine.
Any ideas? (I can put more code up if necessary. Just not sure what would be relevant).
EDIT -- The Fixers output in my rake routes:
fixers GET /fixers(.:format) fixers#index
POST /fixers(.:format) fixers#create
new_fixer GET /fixers/new(.:format) fixers#new
edit_fixer GET /fixers/:id/edit(.:format) fixers#edit
GET /fixers/:id(.:format) fixers#show
PUT /fixers/:id(.:format) fixers#update
DELETE /fixers/:id(.:format) fixers#destroy
Whoa. Just noticed after posting this that the 5th line is missing the "fixer" before the show action line that all my other models have. Why would that have happened? How do I fix it?
EDIT -- I figured it out! Really dumb issue. For some reason, back when I was learning how to do all this, I both included a resources line and added this line above it:
match '/fixer', to: 'fixers#new'
When I took that line out (cause it was redundant), the problem went away. I guess I was messing with the Rails routing automagic. They really do make those defaults the best option.
Hey I have had this same problem before.. and unfortunately have resorted to doing the following: link_to "link text", "/fixers/#{#fixer.id}" I would love to know the actual correct answer for this though.

Resources