在我的Feed中我有这个:
<span class="title"><strong><%= link_to feed_item.title, @micropost %></strong></span><br>
我无法弄清楚如何将其链接到帖子的各个页面。
现在它链接到:http://localhost:3000/microposts
我收到错误:没有路线匹配[GET]“/ microposts”
如果我手动输入网址:http://localhost:3000/microposts/25
我可以看到帖子的个人页面。
这适用于链接到用户个人资料,但我无法将链接用于微博的页面。
&lt;%= link_to feed_item.user.name,feed_item.user%&gt;
我是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
配置/ 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
答案 0 :(得分: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
Rake路线为您的每条路线(如果适用)提供以下信息:
记住这些信息可以简单地查看routes.txt文件中提供的url,以获取我们想要访问的url(/ microposts / 25)。您会看到列出的/microposts/:id(.:format)
网址格式完美处理。瞧,它也会映射到微博#show action你想要的动作,现在只需查看要出现的第一列,您就会看到“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。