ruby on rails app教程无法显示微博页面

时间:2012-06-02 01:25:59

标签: ruby-on-rails-3 model-view-controller oop routing rails-routing

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

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

<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

Microposts Controller

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

Micropost模型

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文件和微博模型

的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]

# 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'

microposts模型按要求...谢谢!

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

1 个答案:

答案 0 :(得分:1)

您收到路线错误 - 没有路线匹配[GET]“/微博。 所以,它与代码无关。只需声明像这样的微博的路线。

配置/ routes.rb中

resources :microposts