Rails显示错误的论坛路径

时间:2016-09-20 09:08:16

标签: ruby-on-rails routing

我制作了属于类别的论坛模型,并且在每个类别的展示视图中需要显示该类别的每个论坛,但链接指向错误的路径(例如,在类别4中,我只有一个论坛指向 category / 1 / forum / 1 和类别1甚至不存在。)

还有一个注意事项:每个类别都有它的位置字段(整数),并且它们按该字段按升序排序。类别4的位置为1,但为什么该数字位于url中类别id的位置?

当我尝试访问/ category / 4 / forum / 1时,我收到错误“无法找到带有'id'的类别= 1”

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

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

1 个答案:

答案 0 :(得分:0)

错误部分解决;我只需要在 ForumsController category_id 用于类别, id 用于论坛)中使用不同的参数。

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

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

并相应更改论坛路线。

看起来问题仍然没有解决。我更改了这些部分,但我仍然遇到URL问题。现在类别和论坛ID在url中混合(而不是 / category / 4 / forum / 1 ,它显示 / category / 1 / forum / 4 )。

编辑:问题终于解决了! This answer让我知道我应该做些什么。我只需要用路径指定@category(只改变两行):

<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 %>