错误:ActionController :: UrlGenerationError in Things#show

时间:2014-10-07 01:11:38

标签: ruby-on-rails ruby ruby-on-rails-4 hyperlink actioncontroller

我有一个has_many:通过模型Thing和Category之间的关联,所以每个人都有另一个。当我创建一个新的Thing并将它与一个Category关联,然后将Category.name放在Thing视图中,然后我尝试将它作为一个链接,它返回错误“ActionController :: UrlGenerationError in Things#show ”。我做错了什么?

事物控制器

class ThingsController < ApplicationController

  def show
    @thing = Thing.find(params[:id])
    @category_things = CategoryThing.all
    @thing.categories.build
  end

  def index
  end

  def new
    @thing = Thing.new
    @things = Thing.all
  end

  def create
    @thing = Thing.new(thing_params)
    if @thing.save
      redirect_to @thing
    else
      render 'new'
    end
  end

  private

    def thing_params
      params.require(:thing).permit(:name, :image_path, :avatar)
    end


  def results
  end

end

类别控制器

class CategoriesController < ApplicationController

  def show
    @category = Category.find(params[:id])
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to @category
    else
      render 'new'
    end
  end

  private

    def category_params
      params.require(:category).permit(:name)
    end

  def results
  end

end

事物模型

class Thing < ActiveRecord::Base
  has_many :category_things
  has_many :categories, :through => :category_things
end

类别模型

class Category < ActiveRecord::Base
  has_many :category_things
  has_many :things, :through => :category_things
end

CategoryThing模型

class CategoryThing < ActiveRecord::Base
  belongs_to :category
  belongs_to :thing
end

Thing.show查看

<div id= "thing">
  <p>
    <% @thing.categories.each do |category| %>
      <%= link_to category.name, category_path(category) %>
      *****[THE ABOVE WORKS WHEN I JUST PUT "category.name"]*****
    <%  end %>
  </p>
</div>

Thing.new View

<h1>Add Something!</h1>
<p>
  <%= form_for @thing, :url => things_path do |f| %>

    <%= f.text_field :name, :placeholder => "Name of the thing" %>
    <br>
    <%= f.label :categories_the_thing_belongs_to %>
    <%= f.collection_select :categories, Category.all, :id, :name %>
    <br>
    <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
</p>

耙路线的输出

                 Prefix Verb   URI Pattern                                        Controller#Action
            ratings_new GET    /ratings/new(.:format)                             ratings#new
         down_votes_new GET    /down_votes/new(.:format)                          down_votes#new
              thing_new GET    /thing/new(.:format)                               thing#new
      good_comments_new GET    /good_comments/new(.:format)                       good_comments#new
     good_comments_show GET    /good_comments/show(.:format)                      good_comments#show
       bad_comments_new GET    /bad_comments/new(.:format)                        bad_comments#new
     related_things_new GET    /related_things/new(.:format)                      related_things#new
             things_new GET    /things/new(.:format)                              things#new
    category_things_new GET    /category_things/new(.:format)                     category_things#new
      thing_ratings_new GET    /thing_ratings/new(.:format)                       thing_ratings#new
   category_ratings_new GET    /category_ratings/new(.:format)                    category_ratings#new
               subjects GET    /subjects(.:format)                                subjects#index
                        POST   /subjects(.:format)                                subjects#create
            new_subject GET    /subjects/new(.:format)                            subjects#new
           edit_subject GET    /subjects/:id/edit(.:format)                       subjects#edit
                subject GET    /subjects/:id(.:format)                            subjects#show
                        PATCH  /subjects/:id(.:format)                            subjects#update
                        PUT    /subjects/:id(.:format)                            subjects#update
                        DELETE /subjects/:id(.:format)                            subjects#destroy
          subjects_show GET    /subjects/show(.:format)                           subjects#show
     subject_things_new GET    /subject_things/new(.:format)                      subject_things#new
             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
                        PATCH  /categories/:id(.:format)                          categories#update
                        PUT    /categories/:id(.:format)                          categories#update
                        DELETE /categories/:id(.:format)                          categories#destroy
     categories_results GET    /categories/results(.:format)                      categories#results
           subjects_new GET    /subjects/new(.:format)                            subjects#new
                   root GET    /                                                  home_page#home
         all_things_new GET    /all/things/new(.:format)                          things#new
          all_allthings GET    /all/allthings(.:format)                           all#allthings
    thing_good_comments GET    /things/:thing_id/good_comments(.:format)          good_comments#index
                        POST   /things/:thing_id/good_comments(.:format)          good_comments#create
 new_thing_good_comment GET    /things/:thing_id/good_comments/new(.:format)      good_comments#new
edit_thing_good_comment GET    /things/:thing_id/good_comments/:id/edit(.:format) good_comments#edit
     thing_good_comment GET    /things/:thing_id/good_comments/:id(.:format)      good_comments#show
                        PATCH  /things/:thing_id/good_comments/:id(.:format)      good_comments#update
                        PUT    /things/:thing_id/good_comments/:id(.:format)      good_comments#update
                        DELETE /things/:thing_id/good_comments/:id(.:format)      good_comments#destroy
     thing_bad_comments GET    /things/:thing_id/bad_comments(.:format)           bad_comments#index
                        POST   /things/:thing_id/bad_comments(.:format)           bad_comments#create
  new_thing_bad_comment GET    /things/:thing_id/bad_comments/new(.:format)       bad_comments#new
 edit_thing_bad_comment GET    /things/:thing_id/bad_comments/:id/edit(.:format)  bad_comments#edit
      thing_bad_comment GET    /things/:thing_id/bad_comments/:id(.:format)       bad_comments#show
                        PATCH  /things/:thing_id/bad_comments/:id(.:format)       bad_comments#update
                        PUT    /things/:thing_id/bad_comments/:id(.:format)       bad_comments#update
                        DELETE /things/:thing_id/bad_comments/:id(.:format)       bad_comments#destroy
          thing_ratings GET    /things/:thing_id/ratings(.:format)                ratings#index
                        POST   /things/:thing_id/ratings(.:format)                ratings#create
       new_thing_rating GET    /things/:thing_id/ratings/new(.:format)            ratings#new
      edit_thing_rating GET    /things/:thing_id/ratings/:id/edit(.:format)       ratings#edit
           thing_rating GET    /things/:thing_id/ratings/:id(.:format)            ratings#show
                        PATCH  /things/:thing_id/ratings/:id(.:format)            ratings#update
                        PUT    /things/:thing_id/ratings/:id(.:format)            ratings#update
                        DELETE /things/:thing_id/ratings/:id(.:format)            ratings#destroy
         thing_up_votes GET    /things/:thing_id/up_votes(.:format)               up_votes#index
                        POST   /things/:thing_id/up_votes(.:format)               up_votes#create
      new_thing_up_vote GET    /things/:thing_id/up_votes/new(.:format)           up_votes#new
     edit_thing_up_vote GET    /things/:thing_id/up_votes/:id/edit(.:format)      up_votes#edit
          thing_up_vote GET    /things/:thing_id/up_votes/:id(.:format)           up_votes#show
                        PATCH  /things/:thing_id/up_votes/:id(.:format)           up_votes#update
                        PUT    /things/:thing_id/up_votes/:id(.:format)           up_votes#update
                        DELETE /things/:thing_id/up_votes/:id(.:format)           up_votes#destroy
                 things GET    /things(.:format)                                  things#index
                        POST   /things(.:format)                                  things#create
              new_thing GET    /things/new(.:format)                              things#new
             edit_thing GET    /things/:id/edit(.:format)                         things#edit
                  thing GET    /things/:id(.:format)                              things#show
                        PATCH  /things/:id(.:format)                              things#update
                        PUT    /things/:id(.:format)                              things#update
                        DELETE /things/:id(.:format)                              things#destroy
            things_show GET    /things/show(.:format)                             things#show
         things_results GET    /things/results(.:format)                          things#results
          things_random GET    /things/random(.:format)                           things#random
            web_console        /console                                           WebConsole::Engine

Routes for WebConsole::Engine:
                          root GET    /                                              web_console/console_sessions#index
         input_console_session PUT    /console_sessions/:id/input(.:format)          web_console/console_sessions#input
pending_output_console_session GET    /console_sessions/:id/pending_output(.:format) web_console/console_sessions#pending_output
 configuration_console_session PUT    /console_sessions/:id/configuration(.:format)  web_console/console_sessions#configuration
              console_sessions GET    /console_sessions(.:format)                    web_console/console_sessions#index
                               POST   /console_sessions(.:format)                    web_console/console_sessions#create
           new_console_session GET    /console_sessions/new(.:format)                web_console/console_sessions#new
          edit_console_session GET    /console_sessions/:id/edit(.:format)           web_console/console_sessions#edit
               console_session GET    /console_sessions/:id(.:format)                web_console/console_sessions#show
                               PATCH  /console_sessions/:id(.:format)                web_console/console_sessions#update
                               PUT    /console_sessions/:id(.:format)                web_console/console_sessions#update
                               DELETE /console_sessions/:id(.:format)                web_console/console_sessions#destroy

的routes.rb

Website::Application.routes.draw do

  get "ratings/new"
  get "down_votes/new"
  get "thing/new"
  get "good_comments/new"
  get "good_comments/show"
  get "bad_comments/new"
  get "related_things/new"
  get "things/new"
  get "category_things/new"
  get "thing_ratings/new"
  get "category_ratings/new"
  resources :subjects
  get "subjects/show"
  get "subject_things/new"
  resources :categories
  get "categories/results"
  get "subjects/new"
  root 'home_page#home'
  get "all/things/new" => 'things#new'
  get "all/allthings"
  resources :things do
    resources :good_comments
    resources :bad_comments
    resources :ratings
      resources :up_votes
    end
  get "things/show"
  get "things/results"
  get 'things/random' => 'things#random'

end

0 个答案:

没有答案