こんにちは私は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