Single Custom Param Name in Routes for Nested Resources Rails 4.1

Override nested named route parameters

If you want :identifier instead of :video_id you will have to code the routes manually. Which is a pain, so you should really consider why you want non-standard param values in your application.

get 'videos/:identifier/images', to: 'images#index', as: 'video_images'

Note that you'll need to do this for all CRUD routes...

get 'videos/:identifier/images/:id', :to => "videos#show", :as => "video_image"
get 'videos/:identifier/images/new', :to => "videos#new", :as => "new_video_image"
post 'videos/:identifier/images', :to => "images#create"
get 'videos/:identifier/images/:id/edit', :to => "images#edit", :as => "ed it_video_image"
put 'videos/:identifier/images/:id', :to => "images#update"
delete 'videos/:identifier/images/:id', :to => "images#destroy"

Rails nested resource parameters

Your task's ID is unique in the tasks table. As long as you want to show, edit or delete a task, this information is enough. You can easily get the parent through your association. But a nested resource allows you to create new tasks. In this case there is no ID set. You need to know the correct parent to set it on your task.

Excerpt from a possible TasksController:

class TasksController < ApplicationController

before_action :set_website, only: [:create]

def create
@website.tasks.create!(task_params)
end

private

def set_website
@website = Website.find(params[:website_id])
end

def task_params
params.require(:task).permit(:title, :text)
end

end

Generated nested routes:

# Routes without :task_id - Parent matters!

website_tasks
GET /websites/:website_id/tasks(.:format) tasks#index
POST /websites/:website_id/tasks(.:format) tasks#create

new_website_task
GET /websites/:website_id/tasks/new(.:format) tasks#new

# Routes with :task_id - Parent redundant.

edit_website_task
GET /websites/:website_id/tasks/:id/edit(.:format) tasks#edit

website_task
GET /websites/:website_id/tasks/:id(.:format) tasks#show
PATCH /websites/:website_id/tasks/:id(.:format) tasks#update
PUT /websites/:website_id/tasks/:id(.:format) tasks#update
DELETE /websites/:website_id/tasks/:id(.:format) tasks#destroy

You can clean the routes from the redundant website_id by using shallow nesting. This is explained in detail in the Rails docs.

Basically it means:

resources :website do
resources :tasks, only: [:index, :new, :create]
end
resources :tasks, only: [:show, :edit, :update, :destroy]

Which is the same as writing:

resources :websites do
resources :tasks, shallow: true
end

There may be some use cases where the full path is valuable, e. g. you want to feed it to a search engine or you want the URL to be more speaking for readers.

How to handle nested resources/routes correctly?

The reason for the missing methods is that form_for in the view are not aware we are nesting. This is fixed by feeding it an array:

form_for([@user, @scaffold])

Rails 4.1 nested routes redirect_to test won't pass

Ok I figured this one out!

The line

post :create, section: attributes_for(:section)

creates a record in the database, which when assigned to a variable, can be used to test with.

section = Section.last

Then going with what Szymon Borucki said, I provided the course_id and the Id needed to call the route.

expect(response).to redirect_to course_section_path(course_id: section.course_id, id: section.id)

AND it works!!

Here is the whole working test!

it "redirects to course_section_path" do
post :create, section: attributes_for(:section)
section = Section.last
expect(response).to redirect_to course_section_path(course_id: section.course_id, id: section.id)
end

Rails 4 Nested Route with Strong Parameters

I think you don't need to call user_params because the request is just a GET request so it doesn't matter what params[:id] and params[:confirmation_code] are. you can simply use these parameters directly regardless if they are strong params or not.

Custom Route to Custom Page in Rails 4

I guess nested resources is what you need. You can specify just index action for nesting and keep all the snippets resources separately. Also you are to add some logic to snippets#index action to check params[:pattern_id]'s existance:

resources :patterns do
# this line will generate the `patterns/:id/snippets` route
# referencing to snippents#index action but within specific pattern.
# Also you have to add pattern_id column to snippets table to define the relation
resources :snippets, only: :index
end
resources :snippets

Use Collection Routes to make Rails to recognize paths like /patterns/:id/snippets/sort

resources :patterns do
resources :snippets, only: :index do
# this line will generate the `patterns/:id/snippets/sort` route
# referencing to snippets#sort action but again within specific pattern.
get :sort, on: :collection
end
end
resources :snippets

If you have Image model you can nest resources the same way like with snippets:

resources :patterns do
resources :images, only: :index
end

If it's just an action in patterns controller you can do:

resources :patterns do
get :images, on: :member
end


Related Topics



Leave a reply



Submit