Rails: deep nested resources gives error: undefined method 'comments' - ruby-on-rails

I'm getting this error undefined method 'comments' in my rails application. I know I shouldn't do a >level 1 nested resources, but I don't know how to apply the correct way in this case.
Currently this is my routes:
resources :performance_indicators do
resources :improvement_actions do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
end
As I said I'm getting this error:
NoMethodError in PerformanceIndicators#show
Showing .../app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments' for nil:NilClass
I don't know if my problem is in the controller. Anyone can help? :)
EDIT:
My comment/_form:
<%= form_for([#performance_indicator, #improvement_action, #improvement_action.comments.build]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my CommentsController:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :set_improvement_action
# GET /comments
# GET /comments.json
def index
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
#comment = #improvement_action.comments.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
#comment = #improvement_action.comments.new(comment_params)
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: #comment }
else
format.html { render :edit }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments).find(params[:improvement_action_id])
end
end
Here Is my PerformanceIndicatorController:
class PerformanceIndicatorsController < ApplicationController
before_action :set_performance_indicator, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /performance_indicators
# GET /performance_indicators.json
def index
#performance_indicators = PerformanceIndicator.all
end
# GET /performance_indicators/1
# GET /performance_indicators/1.json
def show
##performance_indicators = PerformanceIndicator.all.order("created_at DESC")
end
# GET /performance_indicators/new
def new
#performance_indicator = PerformanceIndicator.new
#comments = Comment.new
end
# GET /performance_indicators/1/edit
def edit
end
# POST /performance_indicators
# POST /performance_indicators.json
def create
#performance_indicator = PerformanceIndicator.new(performance_indicator_params)
respond_to do |format|
if #performance_indicator.save
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully created.' }
format.json { render :show, status: :created, location: #performance_indicator }
else
format.html { render :new }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /performance_indicators/1
# PATCH/PUT /performance_indicators/1.json
def update
respond_to do |format|
if #performance_indicator.update(performance_indicator_params)
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully updated.' }
format.json { render :show, status: :ok, location: #performance_indicator }
else
format.html { render :edit }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /performance_indicators/1
# DELETE /performance_indicators/1.json
def destroy
#performance_indicator.destroy
respond_to do |format|
format.html { redirect_to performance_indicators_url, notice: 'Performance indicator was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_comment
#improvement_action = ImprovementAction.find(params[:improvement_action_id])
#comment = Comment.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_performance_indicator
#performance_indicator = PerformanceIndicator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def performance_indicator_params
params.require(:performance_indicator).permit(:name, :numberTimesIdentifiedProblems, :numberTimesAnalysed)
end
end

Lets start by fixing the deep nesting.
resources :performance_indicators, shallow: true do
resources :improvement_actions
end
resources :improvement_actions, only: [] do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
only: [] is a clever trick that nests routes under a resource but suppresses the generation of routes. We want that since the first block actually declares all the routes we need.
Prefix Verb URI Pattern Controller#Action
performance_indicator_improvement_actions GET /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#index
POST /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#create
new_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/new(.:format) improvement_actions#new
edit_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id/edit(.:format) improvement_actions#edit
performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#show
PATCH /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
PUT /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
DELETE /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#destroy
performance_indicators GET /performance_indicators(.:format) performance_indicators#index
POST /performance_indicators(.:format) performance_indicators#create
new_performance_indicator GET /performance_indicators/new(.:format) performance_indicators#new
edit_performance_indicator GET /performance_indicators/:id/edit(.:format) performance_indicators#edit
performance_indicator GET /performance_indicators/:id(.:format) performance_indicators#show
PATCH /performance_indicators/:id(.:format) performance_indicators#update
PUT /performance_indicators/:id(.:format) performance_indicators#update
DELETE /performance_indicators/:id(.:format) performance_indicators#destroy
like_improvement_action PUT /improvement_actions/:id/like(.:format) improvement_actions#upvote
unlike_improvement_action PUT /improvement_actions/:id/unlike(.:format) improvement_actions#downvote
improvement_action_comments GET /improvement_actions/:improvement_action_id/comments(.:format) comments#index
POST /improvement_actions/:improvement_action_id/comments(.:format) comments#create
new_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/new(.:format) comments#new
edit_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id/edit(.:format) comments#edit
improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#show
PATCH /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
PUT /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
DELETE /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#destroy
Un-nesting the resources means there will be a lot less hoops to jump through.
class CommentsController < ApplicationController
before_action :set_improvement_action
# GET /improvement_actions/:improvement_action_id/comments/new
def new
#comment = #improvement_action.comments.new
end
# POST /improvement_actions/:improvement_action_id/comments
def create
#comment = #improvement_action.comments.new(comment_params) do |c|
# #todo - you should associate comment with the user who created it at some point.
# c.author = current_user
end
# note that you where saving the record twice!
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# ...
private
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments)
.find(params[:improvement_action_id])
end
end
You should handle seeding with new records on the controller side if possible.
<%= form_for([#improvement_action, #comment]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Related

Routing with parameters not working on rails

I've been trying to figure this one out but I'm becoming desperate because I don't see why this isn't working.
Whatever I try, no route is matching my link and I get the following error:
Routing Error: uninitialized constant LineItemsController
My link looks like this:
<%= button_to 'Add to template', line_items_path(template_id: #template, position_id: position) %>
So the link being created is:
http://localhost:3000/line_items?position_id=2&template_id=1
routes.rb:
Rails.application.routes.draw do
resources :line_items
resources :templates
resources :positions
line_item_controller.rb
class LineItemsController < ApplicationController
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
#line_items = LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
#line_item = LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
position = Position.find(params[:position_id])
template = Template.find(params[:template_id])
#line_item = LineItem.new(position, template)
respond_to do |format|
if #line_item.save
format.html { redirect_to template_url}
format.js {#current_item = #line_item}
format.json { render action: 'show',
status: :created, location: #line_item }
else
format.html { render action: 'new' }
format.json { render json: #line_item.errors,
status: :unprocessable_entity }
end
end
end
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
def update
respond_to do |format|
if #line_item.update(line_item_params)
format.html { redirect_to #line_item, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
#line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
# def line_item_params
# params.require(:line_item).permit(:position_id, :template_id)
# end
#...
end
From my understanding, my link should send a POST request that should call the create action of the line_item controller, thereby matching the route POST /line_items(.:format) line_items#create
Thanks for the help guys!
I think the issue is the filename of your controller:
line_item_controller.rb should be line_items_controller.rb

No route for post comments

ive been having a problem now for 2 days, i followed many videos and tutorials on how to add comments to a post, and for everyone else it seemed to work smoothly.
I got 4 controllers, Users,Posts,Comments and Walls , basically this site is gonna be a facebook clone. So im displaying everything on the Wall index.html.erb
The error im getting is:
No route matches {:action=>"index", :controller=>"comments", :post_id=>nil} missing required keys: [:post_id]
Routes:
Rails.application.routes.draw do
devise_for :users
resources :uploads
resources :users
resources :posts do
resources :comments
end
resources :walls
root 'walls#index'
end
Comments-controller:
class CommentsController < ApplicationController
before_action :find_post
def index
#comments = Comment.all
end
def new
#post = Post.find(params[:post_id])
end
def create
#comment = #post.comments.create(params[:comment].permit(:content))
#comment.post_id = #post.id
#comment.user_id = current_user.id
#comment.save
# #comment = #post.comments.create(params[:comment].permit[:content])
# #comment.post_id = #post.id
respond_to do |format|
if #comment.save
format.html { redirect_to root_path }
# format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
# def comment_params
# params.require(:comment).permit(:content)
# end
def find_post
#post = Post.find(params[:post_id])
end
end
Posts-controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def new
#post = Post.new
#comment = Comment.new
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
#post.user_id = current_user.id
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.user_id = current_user.id
#post.user = current_user
respond_to do |format|
if #post.save
format.html { redirect_to root_path }
# format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#post.user_id = current_user.id
#post.save
respond_to do |format|
if #post.update(params[:post].permit(:image,:content,:youtube_url))
format.html { redirect_to root_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
format.json { respond_with_bip(#post) }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
format.json { respond_with_bip(#post) }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:content, :image, :username, :avatar, :youtube_url,:image_cache)
end
end
I am rendering the form in the index.html.erb for the wall controller index
<%= render "form" %>
And the form loop
views/walls/_form.html.erb
<div class="comments">
<%= form_for ([#post, #post.comments.build]) do |f| %>
byebug
<%= f.text_area :content, class: 'comments js-auto-size', id: 'alex2' ,:rows => 1 %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
I dont understand why it cant find the post_id though :/
Since your comments is nested inside posts in routes, you have to pass post_id to the index in CommentsController
class CommentsController < ApplicationController
before_action :find_post
def index
#comments = #post.comments
end
end
# in view
<%= link_to "Comments", post_comments_path(#post) %>
Otherwise, you can define comments to be independent resources so you can do it without passing post_id
Rails.application.routes.draw do
resources :posts
resources :comments
end

Rails form not redirecting to Show action

I have three models City, Area and User
In new user form after clicking on submit button server/app is not redirecting to show action
(my routes file config/routes)
Rails.application.routes.draw do
resources :areas
resources :cities
get 'users/update_cities'
resources :users
(user controller app/controllers/users_controller.rb )
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def update_cities
#areas = Area.where("city_id = ?", params[:city_id])
respond_to do |format|
format.js
end
end
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
#cities = City.all
#areas = Area.where("city_id = ?", City.first.id)
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
#user.area_ids = [params[:user][:area_id]]
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :brief,:area_id)
end
end
and form (app/views/users/_form.html.erb)
(labels not included just to clean up d code a bit )
<%= form_for(#user, remote: true) do |f| %>
<%= f.text_field :name %>
<%= f.select :city_id, options_for_select(#cities.collect { |city|
[city.name.titleize, city.id] }), {include_blank: 'Select Something'}, { id: 'cities_select'} %>
<%= f.select :area_id, options_for_select(#areas.collect { |area|
[area.name.titleize, area.id] }, 0), {}, { id: 'areas_select' } %>
<%= f.submit %>
please help.. !
The remote: true parameter in your form is causing the problem. This setting is useful when you want to update/create an object, without updating the view you're in.
For more information you can check ruby on rails API

simple_form is giving error undefined method `reviews_path' for #<# for nested route in Rails 4

I checked the other simple_form posts and they didn't quite hit on my problem. I have a nested resource for restaurant reviews in my routes.rb here:
Rails.application.routes.draw do
devise_for :users
resources :restaurants do
resources :reviews, except: [:show, :index]
end
My review controller seems to be set properly here:
class ReviewsController < ApplicationController
before_action :authenticate_user!
before_action :set_restaurant
before_action :set_review, only: [:edit, :update, :destroy]
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.restaurant_id = #restaurant.id
respond_to do |format|
if #review.save
format.html { redirect_to root_path, notice: 'Review was successfully created.' }
format.json { render :show, status: :created, location: #review }
else
format.html { render :new }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to #review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: #review }
else
format.html { render :edit }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
def set_restaurant
#restuarant = Restaurant.find(params[:restaurant_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
My restaurant controller is here:
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:show, :edit, :update, :destroy]
# GET /restaurants
# GET /restaurants.json
def index
#restaurants = Restaurant.all
end
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/new
def new
#restaurant = Restaurant.new
end
# GET /restaurants/1/edit
def edit
end
# POST /restaurants
# POST /restaurants.json
def create
#restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if #restaurant.save
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully created.' }
format.json { render :show, status: :created, location: #restaurant }
else
format.html { render :new }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /restaurants/1
# PATCH/PUT /restaurants/1.json
def update
respond_to do |format|
if #restaurant.update(restaurant_params)
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully updated.' }
format.json { render :show, status: :ok, location: #restaurant }
else
format.html { render :edit }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /restaurants/1
# DELETE /restaurants/1.json
def destroy
#restaurant.destroy
respond_to do |format|
format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_restaurant
#restaurant = Restaurant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def restaurant_params
params.require(:restaurant).permit(:name, :address, :phone, :website, :image)
end
end
and my simple_form_for is super straight forward:
<%= simple_form_for [#restaurant, #review] do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :rating %>
<%= f.input :comment %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
So, I don't know why I am getting this error :(
You need to provide the value of #restaurant which since you mispelled it as #restuarant will not work.

How do I add a delete route with nested resources rails 4

Todo has many Items. I am trying to add a link to delete an item from the todo list. I have tried variations to find an item by id under the todo list and I can't figure it out. This is the most recent error based of the the changes I made. I don't know how to fix this.
Error message: No route matches [DELETE] "/todos/6/items"
Todo Controller:
class TodosController < ApplicationController
respond_to :html, :js
before_action :set_todo, only: [:show, :edit, :update, :destroy]
# GET /todos
# GET /todos.json
def index
#todos = Todo.all
#todo = Todo.new
end
# GET /todos/1
# GET /todos/1.json
def show
end
# GET /todos/new
def new
#todo = Todo.new
#3.times{#todo.items.build}
end
# GET /todos/1/edit
def edit
end
# POST /todos
# POST /todos.json
def create
#todo = Todo.new(todo_params)
##todo.items.build
respond_to do |format|
if #todo.save
format.html { redirect_to todos_path, notice: 'Todo was successfully created.' }
format.json { render :show, status: :created, location: #todo }
else
format.html { render :new }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todos/1
# PATCH/PUT /todos/1.json
def update
#todo = Todo.find(params[:id])
respond_to do |format|
if #todo.update(todo_params)
format.html { redirect_to #todo, notice: 'Todo was successfully updated.' }
format.json { render :show, status: :ok, location: #todo }
else
format.html { render :edit }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1
# DELETE /todos/1.json
def destroy
#todo.destroy
#todo.items.destroy
respond_to do |format|
format.html { redirect_to todos_url, notice: 'Todo was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo
#todo = Todo.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_params
params.require(:todo).permit(:title, :completed, items_attributes: [:content,:completed, :_destroy])
end
end
Items Controller:
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
before_action :set_todo
respond_to :html, :js
# GET /items
# GET /items.json
def index
#items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
#item = Item.find(params[:id])
end
# GET /items/new
def new
#item = #todo.items.build
end
# GET /items/1/edit
def edit
#item = Items.find(params[:id])
end
# POST /items
# POST /items.json
def create
#item = #todo.items.build(item_params)
respond_to do |format|
if #item.save
format.html { redirect_to [#todo,#item], notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: #item }
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
#item = Item.find(params[:id])
respond_to do |format|
if #item.update(item_params)
format.html { redirect_to [#todo, #item], notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #item }
else
format.html { render :edit }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
#todo.items.destroy
#item = Item.find(params[:id])
#item.destroy
respond_to do |format|
format.html { redirect_to #todo, notice: 'Item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
#item = Item.find(params[:id])
end
def set_todo
#todo = Todo.find(params[:todo_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:content, :todo_id)
end
end
Routes:
Prefix Verb URI Pattern Controller#Action
todo_items GET /todos/:todo_id/items(.:format) items#index
POST /todos/:todo_id/items(.:format) items#create
new_todo_item GET /todos/:todo_id/items/new(.:format) items#new
edit_todo_item GET /todos/:todo_id/items/:id/edit(.:format) items#edit
todo_item GET /todos/:todo_id/items/:id(.:format) items#show
PATCH /todos/:todo_id/items/:id(.:format) items#update
PUT /todos/:todo_id/items/:id(.:format) items#update
DELETE /todos/:todo_id/items/:id(.:format) items#destroy
todos GET /todos(.:format) todos#index
POST /todos(.:format) todos#create
new_todo GET /todos/new(.:format) todos#new
edit_todo GET /todos/:id/edit(.:format) todos#edit
todo GET /todos/:id(.:format) todos#show
PATCH /todos/:id(.:format) todos#update
PUT /todos/:id(.:format) todos#update
DELETE /todos/:id(.:format) todos#destroy
and my index page:
<h1>Listing todos</h1>
<ul>
<% #todos.each do |todo| %>
<li><%= link_to todo.title ,edit_todo_path(todo)%></li>
<ul>
<% todo.items.each do |item| %></br>
<li><%= item.content %></li>
<li><%= link_to 'Delete Item',todo_item_path(item.id), method: :delete, data: { confirm: "Are you sure you want to delete this item?"} %></li>
<% end %>
</ul>
<% end %>
</ul>
<%= link_to 'New Todo', new_todo_path %>
Please try using <%= link_to "delete", [todo, item], :method => :delete %>.

Resources