2016-09-20 5 views
1

カテゴリに属する​​フォーラムモデルを作成しました。すべてのカテゴリを表示するには、そのカテゴリの各フォーラムを表示する必要がありますが、リンクは間違ったパスを指しています(カテゴリ4では、 1つのフォーラムカテゴリ/ 1 /フォーラム/ 1を指し、カテゴリー1は存在しません)。Railsは間違ったフォーラムのパスを示します

もう1つ注意してください:それぞれのカテゴリには位置フィールド(整数)があり、そのフィールドは昇順にソートされています。カテゴリ4の位置は1ですが、その番号はURLのカテゴリIDの場所にあるのはなぜですか?

私がアクセス/カテゴリ/ 4 /フォーラム/ 1にしようとすると、私はエラーが "= 1 'ID' とカテゴリーが見つかりませんでした" 取得

CategoriesController

class CategoriesController < ApplicationController 

    def index 
     categories_ordered 
    end 

    def new 
     @category = Category.new 
     categories_ordered 
    end 

    def create 
     @category = Category.new(category_params) 
     categories_ordered 
     if @category.save 
      redirect_to forums_path, notice: "Kategorija je uspešno kreirana." 
     else 
      render "new" 
     end 
    end 

    def show 
     find_category 
     find_forums 
    end 

    def edit 
     find_category 
    end 

    def update 
     find_category 

     if @category.update(category_params) 
      redirect_to category_path(@category), notice: "Kategorija je uspešno ažurirana." 
     else 
      render "edit" 
     end 
    end 

    def destroy 
     find_category 
     @category.destroy 

     redirect_to forums_path, notice: "Kategorija je uspešno obrisana." 
    end 

    private 

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

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

    def find_forums 
     @forums = @category.forums.all 
    end 

    def categories_ordered 
     @categories = Category.all.order("position ASC") 
    end 

end 

ForumsController

class ForumsController < ApplicationController 

    def new 
     find_category 
     @forum = @category.forums.new 
    end 

    def create 
     @category = Category.find(params[:category_id]) 
     @forum = @category.forums.create(forum_params) 
     if @forum.save 
      redirect_to category_path(@category), notice: "Forum je uspešno kreiran." 
     else 
      render "new" 
     end 
    end 

    def show 
     find_category 
     find_forum 
    end 

    def edit 
     find_category 
     find_forum 
    end 

    def update 
     find_category 
     find_forum 
     if @forum.update(forum_params) 
      redirect_to forum_path(@forum), notice: "Forum je uspešno ažuriran." 
     else 
      render "edit" 
     end 
    end 

    private 

    def forum_params 
     params.require(:forum).permit(:title, :description) 
    end 

    def find_category 
     @category = Category.find(params[:category_id]) 
    end 

    def find_forum 
     @forum = @category.forums.find(params[:id]) 
    end 

end 

カテゴリ#ショー

<% authorize %> 

<h2><%= @category.name %></h2> 

<p><%= @category.description %></p> 

<div class="list-group"> 
    <% @category.forums.each do |f| %> 
     <a href="<%= forum_path(f) %>" class="list-group-item"> 
      <h4 class="list-group-heading"><%= f.title %></h4> 
      <p class="list-group-text"> 
       <%= f.description %> 
      </p> 
     </a> 
    <% end %> 
</div> 

<% if is_admin? %> 

    <%= link_to new_forum_path, class: "btn btn-primary" do %> 
     <span class="glyphicon glyphicon-plus"></span> Dodaj forum 
    <% end %> 

    <%= link_to edit_category_path(@category), class: "btn btn-primary" do %> 
     <span class="glyphicon glyphicon-pencil"></span> Izmeni 
    <% end %> 

    <%= link_to category_path(@category), class: "btn btn-danger", method: :delete, data: { confirm: "Da li ste sigurni da želite obrisati ovu kategoriju?" } do %> 
     <span class="glyphicon glyphicon-trash"></span> Obriši 
    <% end %> 

<% end %> 

routes.rbを

+0

routes.rbファイルを確認しましたか? config/routes.rbも投稿してください。 –

+0

@ChetanDattaが掲載されています。 – Nikola

答えて

0
Rails.application.routes.draw do 

    root "welcome#index" 

    get "start" => "welcome#index", as: "index" 
    get "registration" => "users#new", as: "register" 
    get "login" => "sessions#new", as: "login" 
    post "login" => "sessions#create" 
    get "logout" => "sessions#destroy", as: "logout" 
    get "users" => "users#index", as: "users" 
    post "users" => "users#create" 
    get "profile" => "users#show", as: "profile" 
    get "user/:id" => "users#show", as: "user" 
    get "user/:id/edit" => "users#edit", as: "edit_user" 
    patch "user/:id" => "users#update" 
    delete "user/:id" => "users#destroy" 

    get "categories/add" => "categories#new", as: "new_category" 
    get "category/:id" => "categories#show", as: "category" 
    patch "category/:id" => "categories#update" 
    delete "category/:id" => "categories#destroy" 
    get "category/:id/edit" => "categories#edit", as: "edit_category" 

    get "terms" => "welcome#terms", as: "terms" 
    get "wh" => "bugs#new", as: "wh" 

    get "bugs" => "bugs#index", as: "bugs" 
    post "bugs" => "bugs#create" 
    get "bug/:id" => "bugs#show", as: "bug" 
    delete "bug/:id" => "bugs#destroy" 

    get "forum" => "categories#index", as: "forums" 
    get "category/:category_id/forum/add" => "forums#new", as: "new_forum" 
    get "category/:category_id/forum/:id" => "forums#show", as: "forum" 
    get "category/:category_id/forum/:id/edit" => "forums#edit", as: "edit_forum" 
    patch "category/:category_id/forum/:id" => "forums#update" 
    delete "category/:category_id/forum/:id" => "forums#destroy" 

    resources :users 
    resources :sessions 

    resources :categories do 
    resources :forums 
    end 

    resources :bugs 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 

エラーが部分的に解決されます。カテゴリの場合はForumsControllercategory_id、フォーラムの場合はid)で異なるパラメータを使用しなければなりませんでした。

def find_category 
    @category = Category.find(params[:category_id]) 
end 

def find_forum 
    @forum = @category.forum.find(params[:id]) 
end 

フォーラムルートを適宜変更してください。

まだ問題は解決していないようです。私はこの部分を変更しましたが、私はまだURLに問題があります。今度はカテゴリとフォーラムIDが/カテゴリ/ 4 /フォーラム/ 1の代わりに/カテゴリ/ 1 /フォーラム/ 4と表示されます。

編集:問題はついに解決されました! This answerは私に何をすべきかというアイディアを与えました。 @categoryにもパスを指定するだけでした(2行を変更するだけです):

<div class="list-group"> 
    <% @category.forums.each do |f| %> 
     <a href="<%= forum_path(@category, f) %>" class="list-group-item"> 
      <h4 class="list-group-heading"><%= f.title %></h4> 
      <p class="list-group-text"> 
       <%= f.description %> 
      </p> 
     </a> 
    <% end %> 
</div> 

<% if is_admin? %> 

    <%= link_to new_forum_path(@category), class: "btn btn-primary" do %> 
     <span class="glyphicon glyphicon-plus"></span> Dodaj forum 
    <% end %> 
関連する問題