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
Related
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 %>
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 have added custom routes for the devise actions. this does not work when I try to go to /profile/edit or /login or /logout Here is the rake routes:
saasbook#saasbook:~/Documents/ronde$ rake routes
static_about GET /static/about(.:format) static#about
static_tour GET /static/tour(.:format) static#tour
static_home GET /static/home(.:format) static#home
static_terms_of_use GET /static/terms_of_use(.:format) static#terms_of_use
static_contact GET /static/contact(.:format) static#contact
users_profile GET /users/profile(.:format) registrations#edit
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
register GET /register(.:format) devise/registrations#new
profile_edit GET /profile/edit(.:format) devise/registrations#edit
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/google|facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:google|facebook)
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
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
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / static#home
Here is my routes.rb file where I added the the new routes with the same controller actions for devise:
Ronde::Application.routes.draw do
get "static/about"
get "static/tour"
get "static/home"
get "static/terms_of_use"
get "static/contact"
get "/user/profile", :to => 'registrations#edit'
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
get "profile/edit", :to => "devise/registrations#edit"
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
root to: 'static#home'
end
# :path_names => {:edit => "profile/edit", :sign_in => "login", :sign_out => "logout", :sign_up => "register" }
Then page says that and should route to the devise controller:
Routing Error
No route matches [GET] "/profile/edit"
Try running rake routes for more information on available routes.
I don't know if this right but I think you can't do that way, because Devise doesn't have any controller. Please check this question and this.
What I done usually was making another controller for Devise, or normal controller like users_controller with it own views. And register it on routes.rb like:
devise_for :users
scope "/admin" do
resources :users
end
Then when I need to open it, I called localhost:3000/admin/users
But please correct me if there anything wrong, or my way to do it was wrong. Hope can help.
I have added the omniauth and devise gems and was adding facebook login support. This is my routes.rb file:
resources :authentications do
match 'auth/:provider/callback' => 'authentications/#create', :via => :post
end
I tried post as well as get in the via part.
Here is the controller for authentications controller.
def create
auth = request.env["rack.auth"]
current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
end
Here is the rake routes output
rake routes
authentications GET /authentications(.:format) authentications#index
POST /authentications(.:format) authentications#create
new_authentication GET /authentications/new(.:format) authentications#new
edit_authentication GET /authentications/:id/edit(.:format) authentications#edit
authentication GET /authentications/:id(.:format) authentications#show
PUT /authentications/:id(.:format) authentications#update
DELETE /authentications/:id(.:format) authentications#destroy
conferences GET /conferences(.:format) conferences#index
POST /conferences(.:format) conferences#create
new_conference GET /conferences/new(.:format) conferences#new
edit_conference GET /conferences/:id/edit(.:format) conferences#edit
conference GET /conferences/:id(.:format) conferences#show
PUT /conferences/:id(.:format) conferences#update
DELETE /conferences/:id(.:format) conferences#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
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
GET /conferences(.:format) conferences#index
POST /conferences(.:format) conferences#create
GET /conferences/new(.:format) conferences#new
GET /conferences/:id/edit(.:format) conferences#edit
GET /conferences/:id(.:format) conferences#show
PUT /conferences/:id(.:format) conferences#update
DELETE /conferences/:id(.:format) conferences#destroy
activity_bigbluebutton_server GET /bigbluebutton/servers/:id/activity(.:format) bigbluebutton/servers#activity
rooms_bigbluebutton_server GET /bigbluebutton/servers/:id/rooms(.:format) bigbluebutton/servers#rooms
bigbluebutton_servers GET /bigbluebutton/servers(.:format) bigbluebutton/servers#index
POST /bigbluebutton/servers(.:format) bigbluebutton/servers#create
new_bigbluebutton_server GET /bigbluebutton/servers/new(.:format) bigbluebutton/servers#new
edit_bigbluebutton_server GET /bigbluebutton/servers/:id/edit(.:format) bigbluebutton/servers#edit
bigbluebutton_server GET /bigbluebutton/servers/:id(.:format) bigbluebutton/servers#show
PUT /bigbluebutton/servers/:id(.:format) bigbluebutton/servers#update
DELETE /bigbluebutton/servers/:id(.:format) bigbluebutton/servers#destroy
external_bigbluebutton_rooms GET /bigbluebutton/rooms/external(.:format) bigbluebutton/rooms#external
POST /bigbluebutton/rooms/external(.:format) bigbluebutton/rooms#external_auth
join_bigbluebutton_room GET /bigbluebutton/rooms/:id/join(.:format) bigbluebutton/rooms#join
running_bigbluebutton_room GET /bigbluebutton/rooms/:id/running(.:format) bigbluebutton/rooms#running
end_bigbluebutton_room GET /bigbluebutton/rooms/:id/end(.:format) bigbluebutton/rooms#end
invite_bigbluebutton_room GET /bigbluebutton/rooms/:id/invite(.:format) bigbluebutton/rooms#invite
join_mobile_bigbluebutton_room GET /bigbluebutton/rooms/:id/join_mobile(.:format) bigbluebutton/rooms#join_mobile
POST /bigbluebutton/rooms/:id/join(.:format) bigbluebutton/rooms#auth
bigbluebutton_rooms GET /bigbluebutton/rooms(.:format) bigbluebutton/rooms#index
POST /bigbluebutton/rooms(.:format) bigbluebutton/rooms#create
new_bigbluebutton_room GET /bigbluebutton/rooms/new(.:format) bigbluebutton/rooms#new
edit_bigbluebutton_room GET /bigbluebutton/rooms/:id/edit(.:format) bigbluebutton/rooms#edit
bigbluebutton_room GET /bigbluebutton/rooms/:id(.:format) bigbluebutton/rooms#show
PUT /bigbluebutton/rooms/:id(.:format) bigbluebutton/rooms#update
DELETE /bigbluebutton/rooms/:id(.:format) bigbluebutton/rooms#destroy
/auth/:provider/callback(.:format) authentications/#create
root / profiles#new
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
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
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
This
match 'auth/:provider/callback' => 'authentications/#create', :via => :post
Should be
match 'auth/:provider/callback' => 'authentications#create', :via => :post
Where authentications is controller & create is method