I'm working in a project that has a user to user friendship feature.
I don't know what is happening with my routes, but this is my output in html:
/friends.Friends -> this was supposed to be a link "friends", but when I click on it nothing happens
and
/friend_requests.Friend%20requests -> it was also supposed to be a link "friend_requests"
Rails.application.routes.draw do
root "events#index"
devise_for :users
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "logout", :to => "devise/sessions#destroy"
get "signup", :to => "devise/registrations#new"
end
resources :users, :only => [ :index, :show ]
resources :friendships, :only => [:create, :update, :destroy]
get '/friend_requests', to: 'friend_requests#index'
get '/friends', to: 'friends#index'
resources :events
end
and this is what i did in my view:
<%= link_to friends_path "Friends" %>
<br>
<%= link_to friend_requests_path "Friend requests" %>
This is the output of bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
root GET / events#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
signup GET /signup(.:format) devise/registrations#new
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
friendships POST /friendships(.:format) friendships#create
friendship PATCH /friendships/:id(.:format) friendships#update
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
friend_requests GET /friend_requests(.:format) friend_requests#index
friends GET /friends(.:format) friends#index
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
page GET /*id high_voltage/pages#show
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
When I acces by typing localhost:300/friends or localhost:300/friend_requests it works
but when I try to use link to, it's broken :/
The order of your arguments is not correct.
From the Docs
link_to(name = nil, options = nil, html_options = nil, &block) public
Creates an anchor element of the given name using a URL created by the set of options. See the valid options in the documentation for url_for. It’s also possible to pass a String instead of an options hash, which generates an anchor element that uses the value of the String as the href for the link. Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists). If nil is passed as the name the value of the link itself will become the name.
The name should be passed as first argument but you are passing it as second.
Try to swap the arguments like this:
<%= link_to "Friends" ,friends_path %>
<br>
<%= link_to "Friend requests", friend_requests_path %>
Your link_to helper in the view is not ok,
link_to(name = nil, options = nil, html_options = nil, &block)
<%= link_to "Friends", friends_path %>
<br>
<%= link_to "Friend requests", friend_requests_path %>
Related
So I have created two routes using a 'Member' namespace, when I do rake routes, it does not display the path that I should use it is just showing this;-
GET /user/:id(.:format) member/member#
GET /user/:id/edit(.:format) member/member#edit
when I use this line it returns an error;-
<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "Member/Member", :action => :show} if current_user.picture? %></li>
and get this error;-
No route matches {:action=>"show", :controller=>"Member/Member"}
This is my routes;-
scope module: 'member' do
get '/user/:id', to: 'member#show'
get '/user/:id/edit', to: 'member#edit'
end
My whole rake routes;-
Prefix Verb URI Pattern Controller#Action
charges GET /charges(.:format) charges#index
POST /charges(.:format) charges#create
new_charge GET /charges/new(.:format) charges#new
edit_charge GET /charges/:id/edit(.:format) charges#edit
charge GET /charges/:id(.:format) charges#show
PATCH /charges/:id(.:format) charges#update
PUT /charges/:id(.:format) charges#update
DELETE /charges/:id(.:format) charges#destroy
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_unlock POST /admins/unlock(.:format) devise/unlocks#create
new_admin_unlock GET /admins/unlock/new(.:format) devise/unlocks#new
GET /admins/unlock(.:format) devise/unlocks#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
root GET / public/public#homepage
cart_add_item POST /cart_add_item(.:format) cart/cart#add_item_to_cart
empty_cart GET /empty_cart(.:format) cart/cart#empty_cart
destroy_cart GET /destroy_cart(.:format) cart/cart#destroy
cart GET /cart(.:format) cart/cart#show
product_new GET /product/new(.:format) admin/product#new
product_create POST /product/create(.:format) admin/product#create
product_destroy GET /product/destroy(.:format) admin/product#destroy
GET /user/:id(.:format) member/member#show
GET /user/:id/edit(.:format) member/member#edit
How would I access my show action and my edit action?
I suggest you to use the Rails built-in resources in your routes:
scope module: 'member' do
resources :users, only: [:show, :edit]
end
And then you will be able to call the following paths:
member_user_path(current_user) # /member/users/:id/ -> Show action
edit_member_user_path(current_user) # /member/users/:id/edit -> Edit action
Instead of using the old-fashioned link definition:
{:controller => "member/member", :action => :show}
No route matches {:action=>"show", :controller=>"Member/Member"}
The problem is not with the routes, but with the link itself. You need to change it to below
<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "member/member", :action => :show} if current_user.picture? %></li>
Notice the change :controller => "Member/Member" to :controller => "member/member"
I am doing something like below for versioning in rails 4:
scope '/v1' do
resources :foobar, module: 'v1'
end
scope '/v2' do
resources :foobar, module: 'v2'
end
routes:
/v1/foobar/:id
/v2/foobar/:id
I have the controllers in the app/v1 and app/v2 directories
I am learning ruby on rails by making a project web app,
whenever i go to <%= link_to 'My Profile', user_path(:id) %> error Couldn't find User with 'id'=id" is shown...and url is "http://localhost:3000/users/id
If i convert above link to <%= link_to 'My Profile', user_path %>, it works but now all other pages except user's show page give error No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]....I cant seem to find any solution anywhere...
Here are my configs:
rake routes Prefix Verb URI Pattern Controller#Action
root GET / static#landing
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user GET /users/:id(.:format) users#show
_header.html.erb
<%= link_to 'My Profile', user_path %>
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
root 'static#landing'
devise_for :users
resources :users, :only => [:show]
You need to pass the id of a user to user_path, not the symbol :id - so something like
<%= link_to 'My Profile', user_path(#current_user.id) %>
if the user you want to link to is in an instance variable - or since you are using Devise and it provides the current_user helper you might be wanting
<%= link_to 'My Profile', user_path(current_user.id) %>
I think the first thing wrong is this:
<%= link_to 'My Profile', user_path(:id) %>
You are using a symbol, not a variable
<%= link_to 'My Profile', user_path(#id) %>
Would have an actual variable, assuming you have set #id in your controller.
Normally however you would not set the id for a user in a path - it's a recipe for someone to just change the number manually and view someone elses.
Lets assume you set a session variable called :user_id, then you should do something adding a collection route - so no id is sent.
resources :users do
get :show_profile, on: :collection
end
This gets you a path to your show_profile action, called with
link_to 'My Profile', show_profile_user_path
Then in your controller do your user look up from the session - so something like:
#user = User.find_by id: session[:user_id]
I don't know how devises does things, but please don't trust a user to not fiddle with your parameters!
I'm trying to create a simple search form in my Rails application. I get an error with the url path of the form:
<%= form_tag(med_search, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_to "Search", class: "btn btn-default" %>
<% end %>
The first line above causes error undefined local variable or methodmed_search' for #<#:0x007fab2b5afa90>`
The problem is most likely with my routes setup. I created a new controller action called search so I edited my routes.db to look like this:
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
devise_for :users
#get 'meds/index'
root to: "meds#index"
resources :meds, :path => ''
end
When I do rake routes, I am seeing the path med search so I know the url is valid:
Prefix Verb URI Pattern Controller#Action
med_search GET /meds/:med_id/search(.:format) meds#search
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / meds#index
GET / meds#index
POST / meds#create
GET /new(.:format) meds#new
GET /:id/edit(.:format) meds#edit
GET /:id(.:format) meds#show
PATCH /:id(.:format) meds#update
PUT /:id(.:format) meds#update
DELETE /:id(.:format) meds#destroy
What should I change in the routes to fix this?
Thanks!
EDIT: changed url to med_search_path, get new error: No route matches {:action=>"search", :controller=>"meds"} missing required keys: [:med_id]. Looks like it's related to the route /meds/:med_id/search(.:format)
1) you need to change your form_tag like this
<%= form_tag(search_meds_path, :method => "get", id: "search-form") do %>
2) You need to change your route from member to collection like this
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
3) Not sure why you need to add resources :meds, :path => '' at the bottom again. Incase you dont need it, it is better to remove.
It looks like you're trying to code up a search but because you put it in the resources block Rails is assuming you're talking about a specific med.
Remove the route from the resources block and change it to get 'meds/search' => 'meds#search'. That will allow you to use it as just a regular endpoint without Rails complaining that you need an ID.
the view
<td><%= link_to "+1", :controller => :profiles, :action => :addpoints, :profile_id => profile.id, :task_id => #task.id%></td>
When trying to access profile.id via :profile_id rails returns an error, the id was not passed at all. How do I fix this? What's the best way to pass two params through two models? rake routes
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_dashboard GET /static_pages/dashboard(.:format) static_pages#dashboard
static_pages_pending GET /static_pages/pending(.:format) static_pages#pending
static_pages_taskcompleted GET /static_pages/taskcompleted(.:format) static_pages#taskcompleted
tasks_index GET /tasks/index(.:format) tasks#index
tasks_complete GET /tasks/complete(.:format) tasks#complete
tasks_delete GET /tasks/delete(.:format) tasks#delete
profiles_addpoints GET /profiles/addpoints(.:format) profiles#addpoints
profiles_index GET /profiles/index(.:format) profiles#index
root GET / static_pages#home
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
profiles GET /profiles(.:format) profiles#index
POST /profiles(.:format) profiles#create
new_profile GET /profiles/new(.:format) profiles#new
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
Replace profile.id with #profile.id. Verify that profile is actually an instance. A nil value may be being passed in (as opposed to things being wired incorrectly).
Can we see the method content ?
If the error is about the id, the route is correct. What's in params[:profile_id] ? We need more info here..
I think this is what you wanted to do in Rails 4 way.
<%= link_to "+1", profiles_addpoints_path(:profile_id => profile.id, :task_id => #task.id) %>
That code above will call addpoints method in profiles controller with parameter {:profile_id => profile.id, :task_id => #task.id}. You can access parameter like this.
params[:profile_id] and params[:task_id].
Another thing that's worrysome about your code is that in the routes.rb, profiles_addpoints route is called via GET HTTP verb. This is potentially dangerous because Google search bots will visit these paths and result in a spike in upvotes. Respect HTTP convention and use PUT.
I just started using devise today and i modify session#new.html.erb file to fit my needs. I created a link called Register has follow
<%= link_to "REGISTER", "new_customer_registration_path" %>
However when i click on the link it direct me to a no route errors
Routing Error
No route matches [GET] "/customers/new_customer_registration_path"
Here my rake routes
new_customer_session GET /customers/sign_in(.:format) devise/sessions#new
customer_session POST /customers/sign_in(.:format) devise/sessions#create
destroy_customer_session DELETE /customers/sign_out(.:format) devise/sessions#destroy
customer_password POST /customers/password(.:format) devise/passwords#create
new_customer_password GET /customers/password/new(.:format) devise/passwords#new
edit_customer_password GET /customers/password/edit(.:format) devise/passwords#edit
PUT /customers/password(.:format) devise/passwords#update
cancel_customer_registration GET /customers/cancel(.:format) devise/registrations#cancel
customer_registration POST /customers(.:format) devise/registrations#create
new_customer_registration GET /customers/sign_up(.:format) devise/registrations#new
edit_customer_registration GET /customers/edit(.:format) devise/registrations#edit
PUT /customers(.:format) devise/registrations#update
DELETE /customers(.:format) devise/registrations#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
root / events#index
My route file his has follow
devise_for :customers
resources :events
root :to => 'events#index'
match '/new_customer_registration' => "devise/registrations#new"
I get redirect to this page where the error occurs.
http://localhost:3000/customers/new_customer_registration_path
How can i use it?? Thank in advance
it should not be a string but method
<%= link_to "REGISTER", new_customer_registration_path %>