ActionController::RoutingError (No route matches {:controller=>"users", :action=>"profile"}) - ruby-on-rails

I inherited an old Rails app and I'm really struggling with this error:
ActionController::RoutingError
(No route matches {:controller=>"users", :action=>"profile"}):
app/views/layouts/application.html.erb:59:in
`_app_views_layouts_application_html_erb__105<snip>'
app/controllers/users_controller.rb:40:in `index'
I ONLY get this error when I log in as admin, not as any other user.
Here is my routes.rb
Vvault::Application.routes.draw do
resources :orders
devise_for :users
resources :videos
resources :users
root :to => "users#index"
match 'users/:id/profile' => 'users#profile', :as => :user_profile
end
I think this is the relevant snippet from users_controller.rb:
def index
if current_user.admin?
# admin sees all users
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
else
redirect_to "/users/" + current_user.id.to_s() and return
end
end
I think this is the relevant snippet from application_html.erb:
<div class="sidebar">
<% if user_signed_in? %>
<%= link_to "My account", user_profile_path, :method => :get %>
<% end %>
</div>
If i comment out the third line of application_html.erb I can login as admin, but obviously the profile link does not render for any user.
Any help appreciated.
Thanks!

Try:
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>

Your user_profile_path helper needs an id to be passed in, as the corresponding route has an :id parameter.
The error message is telling you that no route exists for that controller/action combination without any other parameters.
Or, you need to define a route without an id parameter where the controller automatically loads the current user's profile

You need to provide the id to user_profile_path. But since that route points to the user's account, there is no point in setting the id, because it should always be the current user (is that your intention?). If this is the case, then you can rewrite the route as:
match 'users/profile' => 'users#profile', :as => :user_profile
If not, then provide an id to the helper method for your route.

Related

Disable auto redirect_to in rails app

Can I disable auto redirect in ruby on rails, here my example: In my controllers
class WelcomeController < ApplicationController
def index
#a = params[:link]
if #a != nil
Kernel.system("ls")
redirect_to root_path
else
end
end
end
I don't want to redirect to root_path and stay in current pages, so I remove redirect_to root_path, like this:
def index
#a = "abcd"
if #a != nil
Kernel.system("ls")
else
end
end
But my app still auto redirect to root_path, I don't know how to fix that, please! help me:)
my routes:
root 'welcome#index'
match '/upload', to: 'welcome#index', via: 'post'
my html:
<%= form_tag(upload_path, :id => "form_check") do %>
<%= text_field_tag :link %>
<%= submit_tag("Process")%>
<%end%>
I think your problem isn't really auto redirect. You are "redirected" to your index because this line doesn't match: match '/upload', to: 'welcome#index', via: 'post'.
What you should do is to separate index and upload. It will be far easier to manage.
routes:
root 'welcome#index'
get 'form' => 'welcome#form', as: :form_path
post 'upload' => 'welcome#upload', as: :upload_path
form page:
<%= form_tag(upload_path, :id => "form_check") do %>
<%= text_field_tag :link %>
<%= submit_tag("Process")%>
<%end%>
And then you can create your new actions inside welcome, called form and upload. The last one redirect you to /form, so you can see your form again.

twice nested form path issue

i have the following form for my user_calendar model:
<%= form_for([company,user,calendar], :remote => true) do |f| %>
....
<% end %>
route is the following:
scope '(:locale)' do
resources :companies do
resources :users do
resources :user_calendar
end
end
end
When user clicks Edit on users/home:
<%= link_to 'E', edit_company_user_path(user.company, user), :remote => true %>
The method is called from users_controller:
def edit
#calendar = #user.user_calendars.build
respond_to do |format|
format.js
end
end
And edit.js should load the models data for the form:
$('#calendar-form').html("<%= escape_javascript(render :partial => 'user_calendars/form', :locals => { :company => #company, :user => #user, :calendar => #calendar }) %>");
But instead of rendering the form i get the following error:
ActionView::Template::Error (undefined method `company_user_user_calendars_path' for #<#<Class:0x007fc351d12808>:0x00000001cf6128>):
The three #company, #user and #calendar have the right data, but still the path shows the error. Here the result of rake routes on user_calendars controller:
Controller#Action
company_user_user_calendar_index GET (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format) user_calendar#index
POST (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format) user_calendar#create
new_company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/new(.:format) user_calendar#new
edit_company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id/edit(.:format) user_calendar#edit
company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#show
PATCH (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#update
PUT (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#update
DELETE (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#destroy
it could happens because of: resources :user_calendar
if you need resources you should write resources :user_calendars - with (s)
also you could look to guid about resources/resource

form_for method undefined

I read a book called 'Rails 3 in Action' and made two pages: 'index' and 'new', set routes.rb:
root :to => 'projects#index'
match '/new', to:'projects#new'
and projects_controller:
def new
#project = Project.new
end
def create
#project = Project.new(parmas[:project])
#project.save
flash[:notice] = "Project has been created"
redirect_to #project
end
and view files:
index.html.erb
<%= link_to "new", new_path %>
This works correctly, because I end up at localhost:3000/new, but the problem is:
<%= form_for(#project) do |f| %>
This results in:
undefined method `projects_path' for #<#:0x416b830>
Where is projects_path? When I print <%= root_path %>, I get /, but <%= projects_path %> gives error undefined method.
How do I define a method projects_path? Root is not projects_path?
You should define resource for project in routes.rb
resources :projects
This will generate helper projects_path and a banch of others
Remove the line match '/new', to:'projects#new' from routes.rb and add this:
resources :projects
The path used to create new projects is a HTTP post to the index URL of "/projects". You need to specify this route:
post "/projects", :controller => "projects", :action => "create", :as => "projects"
This will generate a projects_path, which is the helper method your form_for is looking for.
As others have said, you should probably use resources :projects instead. If you're interested in only creating a subset of the seven RESTful routes created by default, you can use :only:
resources :projects, :only => %w(index new create)

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

What is wrong in "mapping" URLs to Rails actions with this form?

In 'app/views/users/reset.html.erb' file I have this code:
<%= form_tag( send_reset_users_path, :method => :post ) do %>
<%= text_field_tag :email %>
<%= submit_tag("Send") %>
<% end %>
In 'app/controllers/*users_controller.rb*' I have this code:
def reset
respond_to do |format|
format.html # reset.html.erb
end
end
def send_reset
...
end
In 'config/routes.rb' I have this code:
resources :users do
collection do
get 'reset'
get 'send_reset'
end
end
When I submit the form I get the error: "No route matches "/users/send_reset"" (browser URL becomes '.../users/send_reset'). What is wrong? How can I "map" URLs to Rails actions?
P.S.: I think the problem is in "config/routes.rb"...
the problem is here :method => :post and get 'send_reset', in my opinion you are trying to POST parameters when your conntroller expect GET method
You routes.rb declares the send_reset route as only available via get. You have to write post 'send_reset':
resources :users do
collection do
get 'reset'
post 'send_reset'
end
end

Resources