Ruby on rails Rails显示了错误的论坛路径
我制作了属于类别的论坛模型,在每个类别的show视图中,该类别的每个论坛都需要显示,但链接指向错误的路径(例如,在类别4中,我只有一个指向类别/1/论坛/1的论坛,类别1甚至不存在) 还有一点需要注意:每个类别都有它的位置字段(整数),它们按照该字段的升序排序。类别4有位置1,但为什么那个数字会出现在url中类别id的位置上 当我尝试访问/category/4/forum/1时,出现错误“找不到'id'=1的类别” 分类控制器Ruby on rails Rails显示了错误的论坛路径,ruby-on-rails,routing,Ruby On Rails,Routing,我制作了属于类别的论坛模型,在每个类别的show视图中,该类别的每个论坛都需要显示,但链接指向错误的路径(例如,在类别4中,我只有一个指向类别/1/论坛/1的论坛,类别1甚至不存在) 还有一点需要注意:每个类别都有它的位置字段(整数),它们按照该字段的升序排序。类别4有位置1,但为什么那个数字会出现在url中类别id的位置上 当我尝试访问/category/4/forum/1时,出现错误“找不到'id'=1的类别” 分类控制器 class CategoriesController < Ap
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
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
部分解决了误差;我只需要在ForumsController中使用不同的参数(类别id用于类别,论坛id用于论坛) 并相应改变论坛路线 看来问题还没有解决。我更改了这部分内容,但仍然存在URL问题。现在,在url中混合了类别和论坛ID(而不是显示/category/4/forum/1/category/1/forum/4) 编辑:问题终于解决了!给了我一个应该做的想法。我只需要用路径指定@category(只需更改两行):
多达伊论坛
部分解决了错误;我只需要在ForumsController中使用不同的参数(类别id用于类别,论坛id用于论坛)
并相应改变论坛路线
看来问题还没有解决。我更改了这部分内容,但仍然存在URL问题。现在,在url中混合了类别和论坛ID(而不是显示/category/4/forum/1/category/1/forum/4)
编辑:问题终于解决了!给了我一个应该做的想法。我只需要用路径指定@category(只需更改两行):
多达伊论坛
您是否检查了routes.rb文件??请同时发布您的配置/routes.rb。@ChetanDatta已发布。您是否检查了routes.rb文件??请同时发布您的配置/routes.rb。@ChetanDatta已发布。
<% 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 %>
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
def find_category
@category = Category.find(params[:category_id])
end
def find_forum
@forum = @category.forum.find(params[:id])
end
<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 %>