link_to redirect to a wrong controller - ruby-on-rails-3

I have a layout for my admin area called 'layout_admin' . In 'layout_admin', I have :
<li><%= link_to 'Contenu', :action=>'index', :controller=>'contents' %></li>
<li><%= link_to 'Petitions', :controller => 'petitions', :action => 'index' %></li>
The first link for Contenu works fine, but the second one (for petitions) drives me to a strange error :
Routing Error
No route matches {:controller=>"admin/edito"}
In the address bar I have : localhost:3000/admin/petitions
In routes.rb I have :
namespace :admin do resources :petitions
end
I must also precise that "edito" is another controller outside the admin area which has one action "index". In routes.rb I have get "edito/index" concerning edito_controller.
Does somebody have an idea of the source of the problem ?
Thansk.
Full Rake routes :
temoignages GET /temoignages(.:format) temoignages#index
POST /temoignages(.:format) temoignages#create
new_temoignage GET /temoignages/new(.:format) temoignages#new
edit_temoignage GET /temoignages/:id/edit(.:format) temoignages#edit
temoignage GET /temoignages/:id(.:format) temoignages#show
PUT /temoignages/:id(.:format) temoignages#update
DELETE /temoignages/:id(.:format) temoignages#destroy
admin_petitions GET /admin/petitions(.:format) admin/petitions#index
POST /admin/petitions(.:format) admin/petitions#create
new_admin_petition GET /admin/petitions/new(.:format) admin/petitions#new
edit_admin_petition GET /admin/petitions/:id/edit(.:format) admin/petitions#edit
admin_petition GET /admin/petitions/:id(.:format) admin/petitions#show
PUT /admin/petitions/:id(.:format) admin/petitions#update
DELETE /admin/petitions/:id(.:format) admin/petitions#destroy
admin_contents GET /admin/contents(.:format) admin/contents#index
POST /admin/contents(.:format) admin/contents#create
new_admin_content GET /admin/contents/new(.:format) admin/contents#new
edit_admin_content GET /admin/contents/:id/edit(.:format) admin/contents#edit
admin_content GET /admin/contents/:id(.:format) admin/contents#show
PUT /admin/contents/:id(.:format) admin/contents#update
DELETE /admin/contents/:id(.:format) admin/contents#destroy
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
GET /admin/posts/new(.:format) admin/posts#new
GET /admin/posts/:id/edit(.:format) admin/posts#edit
GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_backend_index GET /admin/backend(.:format) admin/backend#index
POST /admin/backend(.:format) admin/backend#create
new_admin_backend GET /admin/backend/new(.:format) admin/backend#new
edit_admin_backend GET /admin/backend/:id/edit(.:format) admin/backend#edit
admin_backend GET /admin/backend/:id(.:format) admin/backend#show
PUT /admin/backend/:id(.:format) admin/backend#update
DELETE /admin/backend/:id(.:format) admin/backend#destroy
lois_index GET /lois/index(.:format) lois#index
lois_show GET /lois/show(.:format) lois#show
edito_index GET /edito/index(.:format) edito#index
reponses_index GET /reponses/index(.:format) reponses#index
reponses_show GET /reponses/show(.:format) reponses#show
lettres_index GET /lettres/index(.:format) lettres#index
lettres_show GET /lettres/show(.:format) lettres#show
accueils POST /accueils(.:format) accueils#create
new_accueils GET /accueils/new(.:format) accueils#new
edit_accueils GET /accueils/edit(.:format) accueils#edit
GET /accueils(.:format) accueils#show
PUT /accueils(.:format) accueils#update
DELETE /accueils(.:format) accueils#destroy
root / accueil#index

See the route names in the left column of your rake routes? Use that info to build calls to different path helpers:
<li><%= link_to 'Contenu', admin_contents_path %></li>
<li><%= link_to 'Petitions', admin_petitions_path %></li>
In general, that's how you should be building URLs in your Rails 3+ app.
Read through Rails Routing from the Outside In for more information (particularly 2.3: Paths and URLs).
Update:
You should look into using these _path-style helpers on the view that's being pulled up when you visit /admin/petitions.
I bet you have a link on the page similar to this:
<%= link_to "Link Text", :controller => "edito", :action => "index" %>
It's trying to find edito in the admin namespace because that's where you are within the app when you visit /admin/petitions.
To fix, you need to update it to read like this:
<%= link_to "Link Text", edito_index_path %>
Wash, rinse, and repeat for all links, forms, and url_for references in your app.

Related

Rails 3 No route matches {:action=>"show", error

While there are similar questions on this error, most relate to how Rails route handle pluralisation, whereas this is different.
I have a link on the 'show' view page within a controller called MemberPage which I need to link to the 'new' action within a controller called Post
<%= link_to ((content_tag :i, "", :class => "icon-pencil") + content_tag(:span, 'create')), new_members_community_post_path %>
Within routes I have
resources :post
which produces the following related line in rake routes
new_members_community_post GET /members/community/post/new(.:format) members/community/post#new
Hovering over the link shows
127.0.0.1:3000/members/community/post/new
Clicking on it produces the error
No route matches {:action=>"show", :controller=>"members/community/member_page"}
Rails is matching both the wrong controller and the wrong action from that shown in rake routes
I've tried creating explicit match rules in routes.rb, eg (within the members/community namespace)
match '/new-post' => 'post#new'
and also replacing the link_to path with
:controller => :post, :action => :new
but cannot find anything that works

Not redirecting to PUT(Update action) in rails when updating a form

I am stuck with a strange issue. I have googled and tried everything that was possible. All in vain and I am at the same place.
Listing the details
I have a route in routes file as resources :projects
The routes generated is as
admin_projects GET
/admin/projects(.:format) admin/projects#index
POST /admin/projects(.:format) admin/projects#create
new_admin_project GET /admin/projects/new(.:format) admin/projects#new
edit_admin_project GET /admin/projects/:id/edit(.:format) admin/projects#edit
admin_project GET /admin/projects/:id(.:format) admin/projects#show
PUT /admin/projects/:id(.:format) admin/projects#update
DELETE /admin/projects/:id(.:format) admin/projects#destroy*
My form is
form_for [:admin, #project], format: :js,remote: true, html: {id: 'edit-project-form', :method => :put } do |f|
...form fields
<td><%= f.submit 'Save', class: "btn primary save" %></td>
The URL being generated and the form tag on inspection is as follow
IF i manually update this post to put through firebug it gets updated Otherwise its throwing me a no routes matched error
(No route matches [POST] "/admin/projects/46.js"):
Please Help me figuring out whats going wrong.
remove 'format: :js' as 'remote: true' will send this as js request.
that is causing the whole issue.

User Authentication in rails 3.0

When trying to use user authentication I get the following error: "NoMethodError in Viewer#show". And it addresses the error to <%= #page.body.html_safe%> in app/views/viewer/show.html.erb:1:in '_app_views_viewer_show_html_erb__685858346_34780128', which is only one line code by now.
But, when I call login page on browser address bar like: :3000/session/new, it comes Up. Which is not happening with :3000/session/destroy.
It seems that something related to the route is not working properly because, on the other hand, when I call a page on views/layouts/application.htm.erb like <li><%= link_to 'Home', {:controller => 'viewer', :action => 'show', :name => 'home'} %></li> it works, and if I switch to <li><%= link_to 'Home', view_page_path('home') %></li> it gives a similar error.
How can I solve that?
Your use of view_page_path('home') assumes that there is a named path view_page. Changing
get "/:name" => 'viewer#show'
to
get '/:name' => 'viewer#show', :as => :view_page
should fix that.
Secondly when using route helpers with named parameters you need to specify the name so Rails knows what parameters should be used. Change view_page_path('home') to view_page_path(:name => 'home').
And finally a NoMethodError for <%= #page.body.html_safe%> suggests to me that either #page or #page.body is nil.

No route matches :action => edit rails 3

I don’t understand why I am getting the above error message when trying to update a recipe
This is my output of rake routes
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
root / public_pages#index
My controller looks like this
def edit
#recipe = Recipe.find(params[:id])
end
def update
#recipe = Recipe.find(params[:id])
if #recipe.update_attributes(params[:recipe])
redirect_to recipes_path, :notice => "Successfully updated recipe"
else
render :action => 'edit'
end
end
And my link to edit the post
<%= link_to "Edit Recipe", edit_recipe_path(#recipe) %>
the full error is (this is when trying to access the recipes page
Routing Error
No route matches {:action=>"edit", :controller=>"recipes"}
Try running rake routes for more information on available routes.
and finally the form i am using, now the only thing I can think of is that there is an issue with my form, though i thought that you could use the same form for new and edit? though i could be totally wrong
Anyone have any ideas
ok so it seems as if i needed this in my view
<%= link_to "Edit Recipe", edit_recipe_path(r.id) %>
this was because i was passing
<% #recipes.each do |r| %>

Rendered partial not receiving params

I'm trying to implement an 'edit' link that brings up a form to change a displayed attribute on a page.
My layout has:
<div id="company_info">
<%= yield :company_info %>
</div>
<div id="edit_company_info">
</div>
My view has:
<%= content_for :company_info do %>
<%= render 'company_info' %>
<%= link_to "Edit", 'company_info_form', :class => 'btn btn-mini', :method => :get, :remote => true %>
My controller has:
def company_info_form
#company = Company.get(params[:id])
respond_to do |format|
format.js
end
end
My company_info_form.js.erb file has:
$('#edit_company_info').html("<%= escape_javascript(render "company_info_form") %>");
Upon clicking the link, my server shows:
Started GET "/companies/company_info_form" for 127.0.0.1 at 2012-03-12 20:19:13 -0700
Processing by CompaniesController#show as JS
Parameters: {"id"=>"company_info_form"}
Completed 500 Internal Server Error in 1ms
RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
app/controllers/companies_controller.rb:9:in `show'
So I think this is a routing issue-- but I have no idea how to fix it. How do I get the company_id param that is on my current page to be recognized by the partial I'm loading as well?
I'm on /companies/1, but the link is to companies/company_info_form, losing the "company_id = 1" param.
Yes, the issue is with your routes and with your link as you have pointed out.
The first issue can be ascertained as it says Processing by CompaniesController#show as JS. So, its actually going to companies#show where it tries to find a company based on id. But, since no correct id is passed, it errors out.
The second issue is because your link is to companies/company_info_form, as you pointed out, since you have used 'company_info_form' as the path in your link for edit. And you haven't passed current company to the link either.
Since you haven't posted your routes file, which you should have, since you have identified a potential problem with routes , I'll present my own.
In your routes :
resources :companies do
member do
get 'company_info_form'
end
end
That will provide you with
company_info_form_company GET /companies/:id/company_info_form(.:format) companies#company_info_form
Then you can provide the link as :
<%= link_to "Edit", company_info_form_company_path(#company) %>