Previous i used:
data: {autocomplete_source: categories_path} %>
To point to the action index in categories controller.All worked fine!
Now i created an new action in categories controller
def search
#categories = Category.order(:name).where("name like ?", "%#{params[:term]}%")
render json: #categories.map(&:name)
end
And tried to point to that action:
data: {autocomplete_source: search_categories_path} %>
But i get the error:
undefined local variable or method `search_categories_path' for #<#<Class:0x51844c8>:0x5375820>
What did i wrong? Thanks!
My routes:
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
Routes:
Autorails::Application.routes.draw do
resources :products
resources :categories do
collection do
:search
end
end
Check rake routes if that route really exists under that name.
For more information see http://guides.rubyonrails.org/routing.html#path-and-url-helpers
You should have something like this in your routes.rb:
resources :categories do
collection do
get :search
end
end
You should do something like this
resources :categories do
collection do
get :search
end
end
Related
I added resource :products, :path => 'catalog/' to my routes.rb, but my routs look like this:
products POST /catalog(.:format) products#create
new_products GET /catalog/new(.:format) products#new
edit_products GET /catalog/edit(.:format) products#edit
GET /catalog(.:format) products#show
PATCH /catalog(.:format) products#update
PUT /catalog(.:format) products#update
DELETE /catalog(.:format) products#destroy
Why do they have no :ids? For example, product#show should have URI /products/:id(.:format), right?
Also, = link_to products_path(product), class: 'product' do leads me to http://localhost:3000/catalog.1
You should use resources :products instead of resource :proucts. For more info: https://cbabhusal.wordpress.com/2015/10/21/rails-routes-difference-between-resource-and-resources-in-routes-rb/
Assume the following routes:
resources :products do
resources :images
end
This creates the following routes:
product_images GET /products/:product_id/images(.:format) images#index
POST /products/:product_id/images(.:format) images#create
new_product_image GET /products/:product_id/images/new(.:format) images#new
edit_product_image GET /products/:product_id/images/:id/edit(.:format) images#edit
product_image GET /products/:product_id/images/:id(.:format) images#show
PATCH /products/:product_id/images/:id(.:format) images#update
PUT /products/:product_id/images/:id(.:format) images#update
DELETE /products/:product_id/images/:id(.:format) images#destroy
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
Now assume I have a ProductController and a ProductImageController that both inherit from ApplicationController. In ApplicationController I want to be able to have a property called active_product that is automatically set to the relevant product. Something like the following:
def active_product
#product = Product.find(params[:id])
end
The problem here is that sometimes the parameter is called :id and sometimes it's called :product_id. Is there a better pattern for being able to extract the value of the product id?
Sure, how about doing
def active_product
#product = Product.find params[:product_id] || params[:id]
end
I have a drop down menu of all the objects that are part of one model using this code:
<h2>Upload Grades Using "New Grades"</h2>
<%= collection_select :course, :course_id, Course.all, :id,
:name, :prompt => "Select a Course:" %>
<%= link_to 'New Grade', new_grade_path(:course => :course ) %>
In this case my model is Course. How do I access the selection of the user and then pass it to the new.html.erb view. It is to my knowledge that I use the controller to retrieve the value using something like
#course = params[:course][:course_id]
Then in my view, I can display the name of the course with
<%= #course.name %>
inside of the new.html.erb.
And then, if the course has an association like A course has_many students then I can retrieve and display a list of all the students doing something like
#students = #course.students
But, my thinking here is wrong because my code doesn't work. Can someone point out where I'm going wrong here? I've been at this for hours...
Update - included config/routes.rb and rake routes
MygradesSr::Application.routes.draw do
resources :evals
resources :teams
resources :categories
resources :grades
resources :categories
resources :tasks
resources :students
resources :courses
post 'grades/upload_grade'
post 'students/upload_student'
root :to => "home#index"
end
Rake routes:
evals GET /evals(.:format) evals#index
POST /evals(.:format) evals#create
new_eval GET /evals/new(.:format) evals#new
edit_eval GET /evals/:id/edit(.:format) evals#edit
eval GET /evals/:id(.:format) evals#show
PUT /evals/:id(.:format) evals#update
DELETE /evals/:id(.:format) evals#destroy
teams GET /teams(.:format) teams#index
POST /teams(.:format) teams#create
new_team GET /teams/new(.:format) teams#new
edit_team GET /teams/:id/edit(.:format) teams#edit
team GET /teams/:id(.:format) teams#show
PUT /teams/:id(.:format) teams#update
DELETE /teams/:id(.:format) teams#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
grades GET /grades(.:format) grades#index
POST /grades(.:format) grades#create
new_grade GET /grades/new(.:format) grades#new
edit_grade GET /grades/:id/edit(.:format) grades#edit
grade GET /grades/:id(.:format) grades#show
PUT /grades/:id(.:format) grades#update
DELETE /grades/:id(.:format) grades#destroy
GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
GET /categories/new(.:format) categories#new
GET /categories/:id/edit(.:format) categories#edit
GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#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
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
grades_upload_grade POST /grades/upload_grade(.:format) grades#upload_grade
students_upload_student POST /students/upload_student(.:format) students#upload_student
root / home#index
I am trying to add an custom action to a resource but I get a routing error No route matches [GET] "/products/list/up". I have tried commenting out the URI in routes.rb, but they also don't work. What am I doing wrong?
I have this in the routes.rb:
namespace :api, :defaults =>{format: 'json'} do
scope module: :v1 ,constraints: ApiConstraints.new(version:1, default: true) do
resources :products do
member do
match "/list/up" =>"products#product_list" ,:via=>:get
#get "/list/up" , :action=>"product_list"
#get "/list/up" , :to=>"product_list"
end
end
end
end
in the products_controller.rb:
def product_list
#products= Product.all
respond_to do |format|
format.json { render json: #products.to_json}
end
end
Try collection instead of member as you don't provide any id in the path:
resources :products do
collection do # <-----<
get "/list/up" =>"products#product_list"
end
end
Running rake routes gives:
list_up_products GET /products/list/up(.:format) products#product_list
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
I am using Ruby on Rails v3.2.2 and I would like to use plural names for nested resources. That is, in my config/routes.rb I have (note: "category" and "article" are sample resources):
resources :categories do
resources :articles do
collection do
get 'one'
post 'two'
put 'three'
end
member do
get 'four'
post 'five'
put 'six'
end
end
end
The above statements generates the following:
$ rake routes
one_category_articles GET /categories/:category_id/articles/one(.:format) articles#one
two_category_articles POST /categories/:category_id/articles/two(.:format) articles#two
three_category_articles PUT /categories/:category_id/articles/three(.:format) articles#three
four_category_article GET /categories/:category_id/articles/:id/four(.:format) articles#four
five_category_article POST /categories/:category_id/articles/:id/five(.:format) articles#five
six_category_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six
category_articles GET /categories/:category_id/articles(.:format) articles#index
POST /categories/:category_id/articles(.:format) articles#create
new_category_article GET /categories/:category_id/articles/new(.:format) articles#new
edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit
category_article GET /categories/:category_id/articles/:id(.:format) articles#show
PUT /categories/:category_id/articles/:id(.:format) articles#update
DELETE /categories/:category_id/articles/:id(.:format) articles#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
I would like to change statements in my config/routes.rb so to generate following routers with plural names only for the category_article "part" (that is, I would like to use categories_article/categories_articles respectively instead of category_article/category_articles):
$ rake routes
# Note: I marked changes from the previous outputting with '=>'.
=> one_categories_articles GET /categories/:category_id/articles/one(.:format) articles#one
=> two_categories_articles POST /categories/:category_id/articles/two(.:format) articles#two
=> three_categories_articles PUT /categories/:category_id/articles/three(.:format) articles#three
=> four_categories_article GET /categories/:category_id/articles/:id/four(.:format) articles#four
=> five_categories_article POST /categories/:category_id/articles/:id/five(.:format) articles#five
=> six_categories_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six
=> categories_articles GET /categories/:category_id/articles(.:format) articles#index
POST /categories/:category_id/articles(.:format) articles#create
new_category_article GET /categories/:category_id/articles/new(.:format) articles#new
edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit
category_article GET /categories/:category_id/articles/:id(.:format) articles#show
PUT /categories/:category_id/articles/:id(.:format) articles#update
DELETE /categories/:category_id/articles/:id(.:format) articles#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format)
If you aren't satisfied with the provided helpers then you can use the magic:
<%= link_to 'One', [:one, #category, Article] %>
# /categories/123/articles/one
<%= link_to 'Four', [:four, #category, #article] %>
# /categories/123/articles/456/four
<%= link_to 'Edit the article', [:edit, #category, #article] %>
# /categories/123/articles/456/edit
<%= link_to 'All articles for the category', [#category, Article] %>
# /categories/123/articles
The very same approach can be used to specify the :url option for the form_for helper.
I hope you got the idea!
P.S. Note the using of a class (like Article) to specify that you want to see all records and using of a model instance (like #article) to say that you are about to see one article.