カテゴリに属するフォーラムモデルを作成しました。すべてのカテゴリを表示するには、そのカテゴリの各フォーラムを表示する必要がありますが、リンクは間違ったパスを指しています(カテゴリ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を
routes.rbファイルを確認しましたか? config/routes.rbも投稿してください。 –
@ChetanDattaが掲載されています。 – Nikola