0

マイクロポストを気に入ったユーザーを表示するページを表示しようとしています。Uri を localhost:3000/microposts/someid#/into_it にしたいです。パスを介してコントローラに応答していないようですか?

view/microposts/_micropost.html.erb ファイルでこのページにリンクします。

<%= link_to micropost.votes_for, into_it_micropost_path(@micropost) %>

このパスが原因で、ブラウザーに次のエラーが表示され、ページが読み込まれません。

No route matches {:action=>"into_it", :controller=>"microposts", :id=>nil}

私のマイクロポストコントローラーで:

def into_it  #for the view; displays who likes the post
  @title = "Into_it!"
  @micropost = Micropost.find(params[:id])
  render 'show_users_into_it'
end

micropost/show_users_into_it は現在空のファイルです

ルート ファイル:

resources :microposts, only: [:create, :destroy, :into_it] do
  member do
    get :into_it
  end
end

私が持っているレーキルートでは:

into_it_micropost GET    /microposts/:id/into_it(.:format)     microposts#into_it
4

2 に答える 2

1

このエラーは、有効な @micropost ではないために発生します。@micropost は、データベースからのオブジェクトである必要があります。ID が必要です。エラー メッセージで、マイクロポストの ID が nil であることがわかります (@micropost は新しいレコードだと思います)。

:id=>#<Micropost id: nil

既存の @micropost を渡せば問題ありません。

于 2013-01-08T05:55:36.733 に答える
0

本当にばかげた間違いです。ルートが id パラメータを取得していませんでした。私がしなければならなかったことは、単に変更することでした:

into_it_micropost_path(@micropost)

into_it_micropost_path(micropost.id)

データベース内の既存のレコードからIDを取得するように

于 2013-01-08T08:59:54.973 に答える