Rails route problem - ruby-on-rails

class UsersController < ApplicationController
def edit
...
end
Mu routes.rb
match '/user/:id/profile/edit', :to => 'users#edit', :as => "user_profile_edit", :via => "get"
My link:
<%= link_to image_tag("icons/run.png"), user_profile_edit_path %>
Exception:
No route matches {:controller=>"users", :action=>"edit"}
What I'm doing wrong?
Thanks.

You need to supply the user record.
Assuming the record is assigned to #user
<%= link_to image_tag("icons/run.png"), user_profile_edit_path(#user) %>

Related

getting No route matches [POST] "/login/log_in"

im using gem parse-ruby-client and im trying to create a login. and when the login is successful then i want to go to a welcome#index
here is my login_controller.rb
class LoginController < ApplicationController
def index
end
def log_in
#user = Parse::User.authenticate(params[:user][:username], params[:user][:password])
end
end
index.html.erb
<div class="Log_in_Form">
<h4><center>Log in with your existing "app_name" account</center></h4>
<%= form_for(:user, :url => {:controller => 'login', :action => 'log_in'}) do |f| %>
<center><p> Username:</br> <%= f.text_field :username%> </p></center>
<center><p> Password:</br> <%= f.password_field :password%></p></center>
<center><h4><%= f.submit :Login %></h4></center>
<% end %>
</div>
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'login#index'
get 'login/log_in' => 'login#log_in'
end
you need to have an post route or your login. change your routes to this one (if you need the get route also)
Rails.application.routes.draw do
get 'welcome/index'
root 'login#index'
get 'login/log_in' => 'login#log_in'
post 'login/log_in' => 'login#log_in'
end
or change
get 'login/log_in' => 'login#log_in'
to
match 'login/log_in' => 'login#log_in', via: [:get, :post]

Matching a simple route for a form using POST

New to Ruby here.
I keep getting: No route matches {:controller=>"home", :action=>"search"}
I have a simple form in my index view:
<%= form_tag(search_path) do %>
<%= text_field(:search, nil, :placeholder => "yada yada") %>
<%= submit_tag("Search") %>
<% end %>
And I have these routes:
root to: "home#index"
match 'search/:term', to: 'home#search', as: :search, via: [:post]
And the controller: home_controller.rb
class HomeController < ApplicationController
def index
end
def search
render 'index'
end
end
I guess something is wrong with my rout where I try to match 'search/:term', but I can't figure out what.
Your route definition states that you must have a "term" as part of the URL, ie:
http://example.com/search/some+term
Your form is POSTing to the /search patch - but is not providing a "term"
Make the term optional
match 'search(/:term)', to: 'home#search', as: :search, via: [:post]
or remove it all together (you're not referencing it in your form)
match 'search', to: 'home#search', as: :search, via: [:post]
and your problem should go away.

Edit User Info Using Devise and Devise_Invitable

I'm struggling with this issue for the past week or so, and I've tried everything I could think of so I need your help..! I'm using devise and devise invitable
I've created a page to edit user info like username, firstname, lastname...
# /controllers/settings_controllers.rb
class SettingsController < ApplicationController
def info
#user = current_user
end
end
# /controllers/users_controllers.rb
class UsersController < Devise::SessionsController
def update
#user = User.find(current_user.id)
if #user.update_attributes(user_params)
redirect_to :back
end
end
end
# /views/settings/info.html.erb
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :firstname %>
<%= f.text_field :firstname %>
....
<% button_value = "Update" %>
<% end %>
My routes :
devise_for :users ,:controllers => { :invitations => 'users/invitations' }
resources :users, only: [:edit, :update]
devise_for :users, :skip => [:registrations]
as :user do
get 'user/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'user' => 'devise/registrations#update', :as => 'user_registration'
end
devise_scope :user do
authenticated :user do
root :to => 'aggregator#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
get "users/new" => "users#new"
get "users/:id" => "users#show"
end
match 'settings/info' => 'settings#info', :as => 'info'
When I try to update the form, I have the following error (my user id is 1) :
Could not find devise mapping for path "/users/1"
Edit
So I've put resources :users, only: [:edit, :update] after devise_for :users, like suggested by #coletrain and error is gone. But it redirects to my profile /users/1 when I want to be redirected to /settings/info and more importantly, it does not update my info...
My guess is that update method in users_controller is not reached.
In the routes.rb:
add put "users/:id" => "users#update" inside devise_scope :user do ... end block.
Also:
In user_controller update method, change #user.update_attributes(user_params) to #user.update_attributes(params["user"])
I had the same problem. I think the simplest solution is this: Just use what devise gives you by default.
Routes:
devise_scope :user do
get "account", to: "devise/registrations#edit"
patch "account", to: "devise/registrations#update"
put "account", to: "devise/registrations#update"
delete "account", to: "devise/registrations#destroy"
end
/views/devise/registrations/edit.html.erb #generated by devise
replace the path like this:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
to this (because I named the routes "account" in this example)
<%= form_for(resource, as: resource_name, url: account_path, html: { method: :put }) do |f| %>
Note that you have to delete resource_name too. Otherwise there will be routing problems after you committed changes

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

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.

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'

Resources