0

私は、ユーザーがトピックを作成し、他のユーザーがそのトピックに投稿できるアプリケーションを作成しています。

私はこのエラーで立ち往生しています:

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を使用するにはどうすればよいですか?

4

4 に答える 4

1

コレクションが「@topics」であり、各要素が「@topic」ではなく「topic」であるため、機能していません。しかし、あなたは近くにいます。これを試して:

<li><%=link_to topic.title, topic_path(topic) %></li>  
于 2012-06-26T00:43:08.930 に答える
0

何時間も考えた後、今私は自分の間違いを見ることができます。トピックコントローラーでshowメソッドを使用しましたが、ビュー/トピックにshow.html.erbがありませんでした。

トピックを表示する場合は、次の方法を使用する必要があります。

1)config / routers.rbで使用:

match '/topics/:id', to: 'topics#show'

2)私が使用したモデルではbelongs_to:user、:foreign_key => "user_id"

3)リンク:

<li><%=link_to topic.title, **topic_path(topic)** %></li> 

4)ルートで言及したテンプレートを準備します。

これが誰かに役立つことを願っています。

于 2012-06-25T23:44:01.050 に答える
0

これを試して:

<li><%=link_to topic.title, topic_path(:id => @topic.id) %></li>  
于 2012-06-26T00:32:09.387 に答える
0

あなたのルートはおそらく次のように読むべきだと思います:

resources :sessions, :only => [:new, :create, :destroy]
resources :topics, :only => [:show, :create, :destroy]
于 2012-06-26T01:33:15.897 に答える