【问题标题】:link_to post ID in Railslink_to 在 Rails 中发布 ID
【发布时间】:2012-10-15 19:37:20
【问题描述】:

在我的提要中,我有这个:

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

我不知道如何让它链接到帖子的各个页面。

现在它链接到:http://localhost:3000/microposts,我收到一个错误:No route matches [GET] "/microposts"

如果我手动输入 URL:http://localhost:3000/microposts/25 我可以看到帖子的个人页面。

这适用于链接到用户个人资料的链接,但我无法获得链接到微博页面的链接。

我是 Rails 新手,我正在努力解决这个问题。任何帮助将不胜感激。

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/routes.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

【问题讨论】:

    标签: ruby-on-rails-3 link-to


    【解决方案1】:

    长答案

    当您将路由添加到您的 routes.rb 文件时,Rails 会为您生成许多方便的命名路由。通常,当对路线有疑问时,我会查看我的 rake routes 任务,它会显示所有可用路线的列表。尝试运行 rake routes &gt; routes.txt 并打开相应的 routes.txt 文件。

    生成的文件将为您列出一系列请求,在您的情况下,您的 microposts 控制器应该会看到与此类似的内容:

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

    Rake 路线为您的每条路线生成以下信息(如果适用):

    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 时,它会为默认的七个 RESTful 路由(新建、创建、编辑、更新、删除、索引、显示)中的每一个生成路由。在您的情况下,您明确告诉 rails 为创建、销毁和显示操作生成默认路由,以便您可以擦除底部 match "/microposts/:id" => "microposts#show" 的行,因为这已经被处理了。


    简答

    改变这个:

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

    到这里:

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

    请参阅Ruby on Rails Guides: Rails routing from the Outside In,了解有关路线的所有信息。

    【讨论】:

    • 感谢您提供信息丰富的帖子。当我尝试您的简短回答解决方案时,我得到以下操作控制器:异常捕获路由错误:No route matches {:action=&gt;"show", :controller=&gt;"microposts", :id=&gt;#&lt;Micropost id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, title: nil, 我一定在其他地方犯了错误。
    • 实例变量@feed_items 代表什么?这只是您在供稿列表中循环浏览的一系列微博吗?如果不是,微博和提要项之间的关系是什么。如果我猜测一下,您可能想在我提供的简短答案中将@microposts 替换为feed_item
    • @cycle Hunter 是的,提要是一系列微博。我发布了我的 static_pages_controller...不确定这是否会有所作为。
    • @Cycle Hunter...这行得通! 非常感谢。我正在拔头发!
    • 很高兴为您提供帮助,只是尝试在将来与语义和变量命名更加一致以避免混淆自己,为了清楚起见,我什至将@feed_items 重命名为@microposts。如果您有一个名为Bananas 的模型,您可以将许多香蕉的变量简单地表示为“@bananas”而不是“@delicious_yellow_fruits”。
    猜你喜欢
    • 2015-03-21
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多