私は、ユーザーがトピックを作成し、他のユーザーがそのトピックに投稿できるアプリケーションを作成しています。
私はこのエラーで立ち往生しています:
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を使用するにはどうすればよいですか?