0

嗨,我正在学习 Rails 教程,但我已经偏离了教程去做一些不同的事情......我遇到了障碍。因此本教程(michael hartl's)创建了一个微博提要,但它并没有为每个微博提供自己的页面。这就是我正在尝试做的事情,但似乎无法让它发挥作用。

所以在我称之为“活动”的提要视图中,我得到了:

<li id="<%= activity_item.id %>">
  <%= link_to gravatar_for(activity_item.user, :size => 200), activity_item.user %><br clear="all">
  <span class="title"><%= link_to activity_item.title, @micropost %></span><br clear="all">
  <span class="user">
  Joined by <%= link_to activity_item.user.name, activity_item.user %>
  </span><br clear="all">
  <span class="timestamp">
 <%= time_ago_in_words(activity_item.created_at) %> ago.
 </span>
 <% if current_user?(activity_item.user) %>
 <%= link_to "delete", activity_item, :method => :delete,
                                     :confirm => "Are you sure?",
                                     :title => activity_item.content %>
 <% end %>
</li>

当我点击实际的微博“标题”时,我收到以下错误消息,说我有“没有路线匹配 [GET]”/microposts”。我确信这可能是我缺少的一个简单的修复,但我'我是一个初学者,我已经太久了,我的大脑被炸了。

我基本上需要发生的是,当我从我的活动提要中单击微博的标题时...我需要它转到该微博 ID 的唯一 #show 页面。

以下是我认为相关的模型/控制器。如果我需要发布其他任何内容,请告诉我。我感谢任何和所有的帮助!谢谢!

static_pages_controller(主页是我的活动提要显示的地方

class StaticPagesController < ApplicationController

  def home
    if signed_in?
     @micropost = current_user.microposts.build

     @activity_items = current_user.activity.paginate(:page => params[:page])
     @friendactivity_items = current_user.friendactivity.paginate(:page => params[:page])
    end
  end

  def help
  end

  def about
  end

  def contact
  end
end

微博控制器

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

  def index
  end

  def new
   @micropost = current_user.microposts.build if signed_in?
  end

  def create
    @micropost = current_user.microposts.build(params[:micropost])
   if @micropost.save
    flash[:success] = "Micropost created!"
    redirect_to root_path
   else
    @activity_items = [ ]
    render 'new'
   end
  end

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

 def destroy
  @micropost.destroy
  redirect_to root_path
 end

  private

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

end

微柱模型

class Micropost < ActiveRecord::Base
  attr_accessible :content, :title
  belongs_to :user

  validates :title, :presence => true, :length => { :maximum => 100 }
  validates :content, :presence => true, :length => { :maximum => 220 }
  validates :user_id, :presence => true

  default_scope :order => 'microposts.created_at DESC'

  # Returns Microposts from the users that the given user follows
  def self.from_users_followed_by(user)
  followed_user_ids = "SELECT followed_id FROM relationships
                     WHERE follower_id = :user_id"
  where("user_id IN (#{followed_user_ids})",
      :user_id => user.id)
 end

end

我现在还按要求添加了 routes.rb 文件和 micropost 模型

路线.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]

# home page route
root :to => 'static_pages#home'

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

# static pages routes
match '/help', :to => 'static_pages#help'
match '/about', :to => 'static_pages#about'
match '/contact', :to => 'static_pages#contact'

# create a micropost routes
match '/createamicropost', :to => 'microposts#new'

微博模型按要求...谢谢!

class Micropost < ActiveRecord::Base
  attr_accessible :content, :title
  belongs_to :user

  validates :title, :presence => true, :length => { :maximum => 100 }
  validates :content, :presence => true, :length => { :maximum => 220 }
  validates :user_id, :presence => true

  default_scope :order => 'microposts.created_at DESC'

  # Returns Microposts from the users that the given user follows
  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                     WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids})",
          :user_id => user.id)
   end

end
4

1 回答 1

1

您收到路由错误 - 没有路由匹配 [GET] "/microposts。因此,它与代码本身无关。只需像这样声明微帖子的路由。

配置/路由.rb

resources :microposts
于 2012-06-02T08:57:23.143 回答