我正在编写一个应用程序,用户可以创建主题,其他人可以在该主题上发表帖子。
我被这个错误困住了:
No route matches {:action=>"show", :controller=>"topics", :id=>nil}
我的 route.rb :
MyPedia2::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics, only: [:show, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/topics/:id', to: 'topics#show'
我的耙子路线显示:
topics POST /topics(.:format) topics#create
topic GET /topics/:id(.:format) topics#show
DELETE /topics/:id(.:format) topics#destroy
root / static_pages#home
/topics/:id(.:format) topics#show
我的主题控制器是:
# encoding: utf-8
class TopicsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def show
@topic = Topic.find_by_id(params[:id])
end
def create
@topic = current_user.topics.build(params[:topic])
if @topic.save
flash[:success] = "Konu oluşturuldu!"
redirect_to root_path
else
render 'static_pages/home'
end
end
def destroy
@topic.destroy
redirect_to root_path
end
private
def correct_user
@topic = current_user.topics.find_by_id(params[:id])
redirect_to root_path if @topic.nil?
end
end
有解决办法吗?编辑:我发现 _topics.html.erb 失败了我发现是什么破坏了代码:
<% for topic in @topics do %>
<li><%=link_to topic.title, topic_path(@topic) %></li>
<%= will_paginate @topics %>
<% end %>
topic_path(@topic] 部分是错误的。我怎样才能让它使用 id?