I am following the following tutorial, https://www.dailysmarty.com/posts/how-to-add-search-functionality-into-a-rails-api-application .
After managing to complete the entire tutorial, I realised that there are no route that matches "/search". The tutorial did not state how to do routes, hence I have attempted to do one myself by creating the following:
Rails.application.routes.draw do
resources :search, only: [:search]
end
This is not what you want. You need a get route like this:
get '/your_route', to: 'your_controller#your_action'
So, in your case, search is not a resource. So I would use:
get '/search', to: 'search#search'
You can find all this info regarding routing on the rails guide.
I hope this helps!
Update:
As per the tutorial you need to visit yoururl.com/search
In that case, it's better to use the solution provided by #jeremie
get '/search', to: 'search#search'
This will generate
search GET /search(.:format) search#search
The format you are using is incorrect.
only: supports only the seven default actions ->
index, show, new, create, edit, update, and destroy more...
For the custom routes you need to change it to the following
Rails.application.routes.draw do
resources :search, only: [] do
get :search, on: :collection
end
end
This will generate the following route
search_search_index GET /search/search(.:format) search#search
Related
I have done it before but am having trouble adding a new page and a new path to my rails server.
Pretty much, I want to add a new page and then link to that page in a drop down menu... but I am having trouble getting the changes to take effect and for the new path/route to show up when I do "rails routes".
I have done it before for an "offerings" page at pages#offerings but can't seem to figure out how to repeat the same process
I started off going to the pages controller and adding a "def public_speaking" and "end":
Pages Controller
# GET request for / which is our home page
def home
#basic_plan = Plan.find(1)
#pro_plan = Plan.find(2)
end
def about
end
def offerings
end
def public_speaking
end
end
Routes.rb
Then in Routes.rb I tried using the same process (Adding get 'public_speaking', to : 'pages#public_speaking')
root to: "pages#home"
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users do
resource :profile
end
get 'about', to: 'pages#about'
resources :contacts, only: [:create]
get 'contact-us', to: 'contacts#new', as: 'new_contact'
get 'offerings', to: 'pages#offerings'
get 'public_speaking', 'pages#public_speaking'
end
View file
I also created a file "public_speaking.html.erb" in the views folder with the same name.
What am I doing wrong/missing to create this new path? Is there some command to execute this linkage or something?
I expected there to be a new route created (since it worked for "offerings"), however it has not worked and I'm not sure why. I will be repeating this process for 5-6 pages, so I want to be sure I can do it right
i see in your routes, it seems your code is not correct.
you should change:
from get 'public_speaking', 'pages#public_speaking'
to get 'public_speaking', to: 'pages#public_speaking'
Khan Pham has given a correct answer. Seems you are messing with link.
Accordingly to Ruby on rails guide the proper route would be:
get 'public_speaking', to: 'pages#public_speaking'
where to: expects controller#action format.
And then you can check your routes by executing command rake routes and if your part presents there you can use it in your views like:
link_to('Public Speaking', public_speaking_path)
you can read more about url here. Good luck!
Is there a way to use resource routing instead of writing the routes one by one if my methods for which the default expects parameters don't use parameters?
For example, if I had a routes file like below, the expected path for the update method would be like this: /cats/:id (docs)
# routes.rb
Rails.application.routes.draw do
resources :cats, only: [:create, :update]
end
However, I don't require any params for my update method, meaning the path should be /cats.
I know there's a way to rename the params and not use :id, but I didn't find anything on disabling them. I tried adding param: nil to the end of the line but it didn't work.
As I wrote initially, I know this can be done if I write the routes one by one like below. My question is whether I can use resources to do it. Thank you!
# routes.rb
Rails.application.routes.draw do
post 'cats', to: 'cats#create'
put 'cats', to: 'cats#update'
end
This is exactly the use case for singular resources. Quote from the Rails Guides:
Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user.
Change our routing to
resource :cats, only: [:create, :update]
And the following routes will be created:
cats PATCH /cats(.:format) cats#update
PUT /cats(.:format) cats#update
POST /cats(.:format) cats#create
As far as I know, there is not, resource is just a helper to create the standard verb-based CRUD routes, if you want custom routes you need to define your update route the way you did in your second example, of course, you can still use resource for your create route and just pass only: :create.
Have tried using similar questions on SO to fit to my specific case for the past hour or two but it hasn't been working.
I am getting the following error when trying to edit and order but not when I am creating an order which is why I am confused because I use the same form partial for it which starts with <%=form_for(#order, :html => {class: "form-horizontal", role: "form"})do |f|%>
My routes.rb are:
get 'dashboard', to: 'order#index'
get 'order', to: 'order#new'
post 'order', to: 'order#create'
get 'edit/:id', to: 'order#edit', as:'edit'
post 'edit/:id', to: 'order#update'
get 'accept/:id', to: 'order#accept', as: 'accept'
get 'submit/:id', to: 'order#submit', as: 'submit'
I have no clue what's going on because I am used to POST and GET but not PATCH and i've tried researching my issue on my own but I cannot find any solutions.
ANSWER IS IN CHAT IN COMMENTS
for this problem you have to first update your routes as below code
routes.rb
resources :order, except: [:show, :destroy]
create one helper function for add create time and update time link
def order_form_path(object)
object.new_record? ? "/order" : order_path
end
now use above helper function in your form_for partial
<%=form_for(#order,url:order_form_path(#order)%>
hope it will help
your route order.2 means you are doing something like order_path(#someid) in your view but no route is defined for order that require an id...if you need to route specific id do something like this
get 'order/:id' , to: 'controller#action' , as: :order
and then in your views
order_path(#someid)
Patch and put are use to update data not create..
Feel like I'm doing this right, but apparently not.
I have a restful resource, Posts, with index, show, new, update, edit, etc actions in the controller. In routes, I have
resources :posts
I wanted to make the index action occur at the URL '/archive' instead of '/posts'
So I added this line in the routes.rb file, after the resources one:
match '/archive', to: "posts#index"
But when I click on a link to posts_path, it still goes to /post (though if I type in /archive as a url, it works -- not ideal, though). Confused. Could this have to do with my having installed friendly_id?
resources :posts, except: [:index]
get 'archive' => 'posts#index', as: :posts
You need to use something like match '/archive', :to => 'posts#index', :as => 'archived'. Then you will have a new route to the tune of archived_posts_path. The method posts_path does not dynamically changed based on custom matchers. You can always run rake routes to see a list of routes for your site.
I'm having some trouble with using creating my own actions inside a controller I generated using the scaffold.
I understand everything maps to the restful actions but I'm building a user controller where users can login/logout, etc but when I created the action and declared it in the routes.rb I get this error when I visit users/login
Couldn't find User with id=login
It tries to use login as a ID parameter instead of using it as an action.
Routes.rb
match 'users/login' => 'users#login'
I think I'm doing something wrong with the routes so if anybody could help me that would be great.
Thanks
I assume your routes.rb looks like this:
resources :users
match 'users/login' => 'users#login'
The problem is that Rails uses the first route that matches. From the documentation:
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
So either define your custom route before resources :users:
match 'users/login' => 'users#login'
resources :users
…or use this syntax for adding more RESTful actions:
resources :users do
collection do
match 'login'
end
end
To see the existing routes (and their order) run rake routes from the command line.