Rails App on Ubuntu running Apache and Passenger throws 404 with params - apache

I am trying to set up a server to host my rails apps. I have one I am testing and it is giving me a 404 error when I call a controller action while passing params in the URL. I have this app deployed on Heroku and it runs perfectly. Any action that is called without params work perfectly on the new server. It is only the action that passes the params that is giving me the 404.
Also I ran it locally with Webrick on the server and it works fine. It only has this issue when running on passenger.
Any ideas what might cause this or where I should start to find the issue?
This is the request being sent
Request GET /groups/2/set_and_redirect/%2Fgroups%2F2%2Fgroups_lists HTTP/1.1
This is the body of the 404 response
The requested URL /groups/2/set_and_redirect//groups/2/groups_lists was not found on this server.
Edit
This is the partial that is causing the issue. Specificaly the link_to group.name. The edit and delete both work fine and the set and redirect works fine on Heroku and Webrick.
<% #groups.each do |group| %>
<h4><div>
<%= link_to group.name, groups_set_and_redirect_path(:id => group.id, :path => group_list_path(id: group.id) ), remote: true %>
<%= link_to ('<i class="glyphicon glyphicon-edit"></i>').html_safe, edit_group_path(group),'data-toggle' => 'modal', 'data-target' => "#group-modal#{group.id}", remote: true %>
<%= link_to ('<i class="glyphicon glyphicon-trash text-danger"></i>').html_safe, group, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
</div></h4>
<%end%>
<div> <%= link_to ('<i class="glyphicon glyphicon-plus text-success"></i>').html_safe, "#group-modal", 'data-toggle' => 'modal', class: 'btn btn-lg' %> </div>
I changed my route from
get 'groups/:id/set_and_redirect/:path' => 'groups#set_and_redirect',
:as => :groups_set_and_redirect
to
get 'groups/:id/set_and_redirect' => 'groups#set_and_redirect', :as =>
:groups_set_and_redirect
and now it works

Adjust your routes.rb so that a double slash doesn't occur (perhaps by using a named parameter for path).

Related

Rails 3 Search Action Redirects To Show?

I've ran into a weird problem in Rails. When I try and submit a search query with the following form on my Uploads controller:
<%= form_tag ({:controller => "uploads", :action => 'find_uploads'}), :method => "get" do %>
<%=h text_field_tag :search, params[:search], :id => 'search_field' %>
<br />
<%= submit_tag "Search", :style=>"display:inline;" %>
<% end %>
I get redirected to the following url and error page:
/uploads/find_uploads?utf8=✓&search=bot&commit=Search
ActiveRecord::RecordNotFound in UploadsController#show
Couldn't find Upload with id=0
My find_uploads route: get 'uploads/find_uploads' => "uploads#find_uploads"
And when I did I rake routes this is what I got:
uploads_find_uploads GET /uploads/find_uploads(.:format) {:controller=>"uploads", :action=>"find_uploads"}
Everything seems to be in order... not sure why it's not working out as expected. For debugging purposes I dropped breakpoints in both my find_uploads and show actions and neither of them were reached so this error message must be lying to me as the UploadsController show action is never called!
This form is being rendered on my index page if it counts for anything.
I think it's taking find_uploads for an id.
Declare your routes like that:
resources :uploads do
collection do
get :find_uploads
end
end
ps: currently, when you do rake routes, /uploads/find_uploads is after /uploads/:id right?

devise rails - error messages in different view?

I'm trying to show my errors (both flash and devise) in one of my layout files (_header.html.erb):
<div id="alerts">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<% end %>
<%= devise_error_messages! %>
</div>
but I'm getting an error: undefined local variable or method `resource', because the error message is no longer in the devise form. I tried the method suggested here: http://pupeno.com/2010/08/29/show-a-devise-log-in-form-in-another-page/ by pasting that code into the application_controller.rb file. No luck. Ideas?
oh. I forgot to mention... The pages work without error, but my tests are failing... Here's a failing test:
it 'succeeds with a valid email and password' do
user = Factory.create(:user)
visit sign_in_path
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => user.password
click_button 'Sign in'
page.should have_content("hi #{user.username}")
end
This is Rails3, fyi.
I was getting errors like this and solved using :partial and :locals when using the render method:
<%= render :partial => 'header', :locals => { :flash => flash } %>
See Rails 3: "undefined local variable or method" if I put content in a partial
turns out the code needed to be in the application_helper instead of the controller, but the code at pupeno.com works!

Destroy action in a Rails 3.0 app with jquery mobile is producing a routing error

I'm using jquery mobile in a rails 3.0 app. I am getting a routing error when attempting to delete an item from a user's list. The code and error message are as follows.
index.mobile.erb:
<% #items.each do |i| %>
...
<div data-role="button">
<%= link_to 'Delete', i, :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
routes file:
resources :items, :except => [:show]
rake routes:
DELETE /items/:id(.:format) {:action=>"destroy", :controller=>"items"}
error message:
Started GET "/items/2" for 127.0.0.1 at 2012-02-26 13:31:37 -0500
ActionController::RoutingError (No route matches "/items/2"):
What am I missing?
Thanks!
UPDATE - Firebug:
#http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:641 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:2737 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:3030 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:3393 #http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:2926 #http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:2560
jquery...0343332 (line 641)
"NetworkError: 404 Not Found - http://localhost:3000/items/2"
Seems like the problem you're having is the same as mentioned # Rails' link_to method: GETing when it should DELETE.
Can you paste rest of the request parameters (chrome developer console or firebug)? Have to see if rails is trying to do magic to accomodate the HTTP delete verb.

Simple Rails 3 Route is Failing - No route matches

In my config/routes.rb I have:
post "portal_plan_document/update"
rake routes confirms this:
$ rake routes
portal_plan_document_update POST /portal_plan_document/update(.:format) {:controller=>"portal_plan_document", :action=>"update"}
....
In my code I have:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:method => "POST", :remote => true do |f| %>
In my log file I see:
Started POST "/portal_plan_document/update" for 127.0.0.1 at 2011-03-31 18:04:37 -0400
ActionController::RoutingError (No route matches "/portal_plan_document/update"):
I am lost as what to do from here. Any help would be greatly appreciated!
I should state I am using Ruby 1.9.2 and Rails 3.0.5. Oh, I have restarted the server (WebBrick w/rails server) after updating routes.rb.
Jeremy
Figured it out! :)
if you have a non-empty object, rails assumes you want to update that object. i.e., use a 'PUT' instead of a 'POST'
to accomplish 'PUTs', rails will put a hidden input in the form, with "_method" = "put". so, it LOOKS like it's a POST, but rails is treating it as a PUT.
if you actually want to just update an object (what it looks like you're doing), a PUT is better, and you should just switch your routes over to PUT.
if (like I was), you're doing something that really requires a POST (i.e., it can't be sent more than once safely), you can write your form_for like this:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:html=>{:method => "POST"}, :remote => true do |f| %>
to confirm, look at the generated HTML source, and make sure the hidden "_method" field is not set to "put"
Try using that instead please :
:method => :post
If this does not still work, please lose the remote attribute and give it a try. Does it work without it ?
I had the same problem while upgrading a simple app from Rails 2 to Rails 3.
As you may guess I was updating all "remote_form_for(#item) (..)" helpers to "form_for :item remote => true (..)" syntax.
In my case this code from a items/_new.html.erb partial:
<%= form_for :item, :remote => true do |f| %>
<!--FIELDS-->
<% end %>
Gave me this error:
Started POST "/items/new" for 127.0.0.1 at Fri Aug 12 18:19:23
+0200 2011
ActionController::RoutingError (No route matches "/items/new")
As you can notice the method was a correct "POST", not a "PUT". The problem lied in the routing... I don't know why but when a remote POST method is sent by a partial, Rails routes the POST request to "/items/new" instead of "/items" route. Even if the purpose is to create a new "item" so the POST request should be correctly (and RESTfully) routed to "/items".
This code, with explicit action and controller, solved the problem:
<%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" } do |f| %>
<!--FIELDS-->
<% end %>

Routing error on Rails 3

I'm working on a profile page, so the config is like this:
routes.rb
=> match "user/:login(*path)" => 'users#profile', :as => :profile
rake routes
=> profile /user/:login(*path)(.:format) {:action=>"profile", :controller=>"users"}
on console
> Rails.application.routes.recognize_path("/user/example/whatever")
=> {:action=>"profile", :login=>"example", :controller=>"users", :path=>"/whatever"}
And I have a profile action in UsersControllers.
But when I use
&lt%= link_to user.name, profile_path(user.login) %>
in a view I get the error
No route matches {:login=>"example", :controller=>"users", :action=>"profile"}
What am I missing?
Thanks
Update:
Thanks for the answer and attention, Steve!
After a lot of time trying, a coworker find what I was missing: the problem was only with some logins that are emails too, with "#", ".", etc. The solution was adding to_url at params[:login] in link_to:
&lt%= link_to 'name', profile_path(params[:login].to_url) %>
Again, thanks for the attention!
Using your configuration, the route and helper seem to work fine in a Rails 3.0.5 sample app.
I verified the route helper profile_path in the Rails console:
>> app.profile_path('example')
=> "/user/example"
and checked that it worked in the view as well:
<%= link_to 'name', profile_path(params[:login]) %>
No route errors in either place. And putting <%= debug(params) %> in the view shows the path '/user/example/whatever' is being parsed correctly:
--- !map:ActiveSupport::HashWithIndifferentAccess
controller: users
action: profile
login: example
path: /whatever