0

私のフィードにはこれがあります:

<span class="title"><strong><%= link_to feed_item.title, @micropost %></strong></span><br>

投稿の個々のページにリンクする方法がわかりません。

現在、次のリンクにリンクしていhttp://localhost:3000/micropostsます:エラーが発生しました:[GET]"/microposts"に一致するルートがありません

URLを手動で入力した場合:http://localhost:3000/microposts/25投稿の個別のページが表示されます。

これはユーザープロファイルへのリンクで機能しますが、マイクロポストのページへのリンクを機能させることができません。<%= link_to feed_item.user.name、feed_item.user%>

私はレールに不慣れで、これを理解しようとしています。どんな助けでもいただければ幸いです。

microposts_controller.rb

class MicropostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user,   only: :destroy

  def index
  end

  def show
    @micropost = Micropost.find(params[:id])
  end

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    redirect_to root_url
  end

  private

  def correct_user
    @micropost = current_user.microposts.find_by_id(params[:id])
    redirect_to root_url if @micropost.nil?
  end
end

config / routers.rb

SampleApp::Application.routes.draw do
  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :sessions, only: [:new, :create, :destroy]
  resources :microposts, only: [:create, :destroy, :show]
  resources :relationships, only: [:create, :destroy]

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
end

_feed_item.html.erb

<li id="<%= feed_item.id %>">
  <%= link_to gravatar_for(feed_item.user), feed_item.user %>
  <span class="title"><strong><%= link_to feed_item.title, micropost_path(@micropost) %></strong></span><br>
    <span class="user">
      <p><small>Created by: <%= link_to feed_item.user.name, feed_item.user %><br>
        <%= feed_item.loc1T %><br>
         <%= feed_item.startTime.strftime('%A, %B %d, %Y') %></small></p>
    </span>
    <span class="content"><%= feed_item.content %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
    </span>
  <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete,
                                     data: { confirm: "Are you sure?" },
                                     title: feed_item.content %>
  <% end %>
</li>

feed.html.erb

<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render partial: 'shared/feed_item', collection: @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>`

static_pages_controller

class StaticPagesController < ApplicationController
  def home
    if signed_in?
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def help
  end

  def about
  end

  def contact
  end
end
4

1 に答える 1

3

長い答え

ルートをroutes.rbファイルに追加すると、Railsは便利な名前付きルートをいくつか生成します。通常、ルートに疑問がある場合は、使用可能なすべてのルートのリストを表示するレーキルートタスクを確認します。rake routes > routes.txt実行して、対応するroutes.txtファイルを開いてみてください。

結果のファイルには、一連のリクエストが一覧表示されます。この場合、マイクロポストコントローラーに次のようなものが表示されます。

          POST      /microposts(.:format)                microposts#create
micropost GET       /microposts/:id(.:format)            microposts#show
          DELETE    /microposts/:id(.:format)            microposts#destroy

レーキルートは、ルートごとに次の情報を生成します(該当する場合)。

  1. ルート名(ある場合)
  2. 使用されるHTTP動詞(ルートがすべての動詞に応答しない場合)
  3. 一致するURLパターン
  4. ルートのルーティングパラメータ

その情報を念頭に置いて、routes.txtファイルで提供されているURLを調べて、取得しようとしているURL(/ microposts / 25)を確認してください。/microposts/:id(.:format)リストされているURLパターンがそれを完全に処理していることがわかります。見よ、それはあなたが望むmicroposts#showアクションにもマップするので、名前付きルートを取得するには、表示される最初の列を見るだけで、「microposts」キーワードが表示されます。これに_path`を追加するだけで、名前付きルートをビューで使用してリンクURLを生成できます。この特定のルートにはidパラメーター(URLパターンで詳しく説明されています)が必要なため、名前付きルートヘルパーとid引数も渡す必要があります。

また、routes.rbファイルに追加するresources :somethingと、デフォルトの7つのRESTfulルート(new、create、edit、update、delete、index、show)ごとにルートが生成されます。あなたの場合、作成、破棄、表示のアクションのデフォルトルートを生成するようにレールに明示的に指示している match "/microposts/:id" => "microposts#show"ので、すでに処理されているため、下部の行を消去できます。


短い答え

これを変える:

<%= link_to feed_item.title, @micropost %>

これに:

<%= link_to feed_item.title, micropost_path(feed_item) %>

ルートについて知る必要があるすべてについては、Ruby on Railsガイド:OutsideInからのRailsルーティングを参照してください。

于 2012-10-15T22:03:37.383 に答える