Umesh bhambri
Umesh bhambri

Reputation: 1

routes error in rails

I am working on a project. I am making an application where a user can add an issue (like a post) and the user can comment on it.

on running this application, I get an error Couldn't find Issue with 'id'=show the code for routes file is

 resources :issues do
    resources :comments
  end

  get 'users/new'

  get 'users/create'

  get 'users/show'

  get 'users/edit'

  get 'issues/show/:id', :to => 'issues#show'

  resources :users
  resources :sessions, :only => [:create, :new,:destroy]




  get '/signup', :to => 'users#new'
  get '/signin' , :to => 'sessions#new'
  get '/signout', :to => 'sessions#destroy'

the code for the issues controller is

class IssuesController < ApplicationController


    def new
        @issue = Issue.new
    end


    def create
        @issue = Issue.new(issues_params)
        if @issue.save
            flash[:success]='your issue has been raised'
            redirect_to :controller => 'issues', :action => 'show', :id => @issue.id 
        else 
            render 'new'
        end

    end

    def edit
        @issue = Issue.find(params[:id])

    end

    def update
        @issue = Issue.find(params[:id])
        if @issue.update_attributes(issues_params)
            redirect_to :controller => 'issues', :action => 'show', :id => @issue.id        
        else 
            render 'edit'
        end 
    end

    def index
        @issues = Issue.all

    end 


    def show

        @issue = Issue.find(params[:id])


    end

    def destroy
        @issue=Issue.find(params[:id])
        @issue.destroy
        redirect_to :controller => 'issues', :action => 'index'
    end








    protected
        def issues_params
        params.require(:issue).permit(:title,:content)
        end


end

the code for the comments controller from where I call the show method in issues controller is

class CommentsController < ApplicationController


def create 
    @issue = Issue.find(params[:issue_id])
    @comment = @issue.comments.create(comment_params)

    if @comment.save
        redirect_to :controller => 'issues', :action => 'show', :id => @issue[:id]
    else 
        render 'new'
    end
end 





private
    def comment_params
        params.require(:comment).permit(:content)

    end


end

Upvotes: 0

Views: 41

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Change this

resources :issues do
  resources :comments
end

to

resources :issues, except: [:show] do
  resources :comments
end

It will resolve your problem!

Upvotes: 1

smathy
smathy

Reputation: 27961

You must be trying to request the URI /issues/show? This will map to the GET /issues/:id from the resources :issues do line of your routes. The router will set the params[:id] to the string "show" and send the request to the show action of the IssuesController which, as you've shown, will then try to do Issue.find(params[:id]) ie. Issue.find("show") and hence you get your error.

Upvotes: 1

Related Questions