2012-03-08 15 views
1

私はrailsspace帳を以下ですが、私は編集ショーをしようとしたとき、ポストの所有者(チャプター15)を除去私が取得:エラーメッセージ未定義のメソッド「タイトル」:NilClass

undefined method `title' for nil:NilClass 

次のextact:

1: <div class="post"> 
2: <div class="post_title"> 
3: 
4: <%= sanitize post.title %> 
5: <% unless hide_edit_links? %> 
6: <span style="float:right"> 
7: <%= link_to_unless_current 'Mostrar', blog_post_path(post.blog, post) %> | 

ルビー1.9.2 レール3.1.3

コントローラ、ビュー及びルートは次の通りである:

コントローラ:posts_controller

# Encoding: UTF-8 
class PostsController < ApplicationController 
    helper :profile 
    before_filter :protect, :protect_blog 
    # GET /posts 
    # GET /posts.xml 
    def index 
    #Recheck implement paginate for Rails 3.1.3 
    #@pages, @posts = paginate(@blog.posts) 
    @posts = @blog.posts 
    @title = "Administración del Blog" 
    respond_to do |format| 
     format.html # index.rhtml 
     format.xml { render :xml => @posts.to_xml } 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.xml 
    def show 
    @post = Post.find(params[:id]) 
    @title = @post.title 
    respond_to do |format| 
     format.html # show.rhtml 
     format.xml { render :xml => @post.to_xml } 
    end 
    end 

    # GET /posts/new 
    def new 
    @post = Post.new 
    @title = "Nuevo post" 
    end 

    # GET /posts/1;edit 
    def edit 
    @post = Post.find(params[:id]) 
    @title = "Edit #{@post.title}" 
    end 

    # POST /posts 
    # POST /posts.xml 
    def create 
    @post = Post.new(params[:post]) 
    @post.blog = @blog 

    respond_to do |format| 
     if @post.duplicate? or @blog.posts << @post 
     flash[:notice] = 'Post was successfully created.' 
     format.html { redirect_to blog_post_url(:id => @post) } 
     format.xml { head :created, :location => blog_post_url(:id => @post) } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @post.errors.to_xml } 
     end 
    end 
    end 

    # PUT /posts/1 
    # PUT /posts/1.xml 
    def update 
    @post = Post.find(params[:id]) 
    respond_to do |format| 
     if @post.update_attributes(params[:post]) 
     flash[:notice] = 'Post was successfully updated.' 
     format.html { redirect_to post_url(:id => @post) } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @post.errors.to_xml } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.xml 
    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url } 
     format.xml { head :ok } 
    end 
    end 

    private 

    # Ensure that user is blog owner, and create @blog. 
    def protect_blog 
    @blog = Blog.find(params[:blog_id]) 
    user = User.find(session[:user_id]) 
    unless @blog.user == user 
     flash[:notice] = "That isn't your blog!" 
     redirect_to hub_url 
    return false 
    end 
    end 
end 

ビュー:インデックス

<h2>Sus Blog Posts</h2> 
<p class="edit_link"> 
<%= link_to 'Agregar nuevo post', new_blog_post_path %> 
<%= "| Pages: #{pagination_links(@pages)}" if paginated? %> 
</p> 
<%= render :partial => "post", :collection => @posts %> 

ビュー:このビューでポスト

<div class="post"> 
<div class="post_title"> 

<%= sanitize post.title %> 
<% unless hide_edit_links? %> 
<span style="float:right"> 
<%= link_to_unless_current 'Mostrar', blog_post_path(post.blog, post) %> | 
<%= link_to_unless_current 'Editar', edit_blog_post_path(post.blog, post) %> | 
<%= link_to 'Eliminar', blog_post_path(post.blog, post), 
:confirm => 'Deseas eliminar este post?', :method => :delete %> 
</span> 
<% end %> 
</div> 
<div class="post_body"><%= sanitize post.body %></div> 
<div class="post_creation_date"> 
Publicado <%= time_ago_in_words post.created_at %> ago 
<% if post.updated_at != post.created_at %> 
<br /> Modified <%= time_ago_in_words post.updated_at %> ago 
<% end %> 
</div> 
</div> 

、私は、変数のポスト= @ポストでプット@をtriyingたが、それは動作しません ルート

Expression::Application.routes.draw do 

    get "email/remind" 

    get "avatar/index" 

    get "avatar/upload" 

    get "avatar/delete" 

    get "community/index" 

    get "community/browse" 

    get "community/search" 

    get "faq/index" 

    get "faq/edit" 

    get "spec/index" 

    get "spec/edit" 

    get "profile/index" 

    get "profile/show" 

    get "user/index" 

    get "user/register" 

    get "site/index" 

    get "site/about" 

    get "site/help" 

    get "user/login" 

    get "user/logout" 

    get "user/edit" 

    get "user_mailer/welcome_email" 

    post "user/register" 

    post "user/login" 

    post "user/edit" 

    post "spec/edit" 

    post "faq/edit" 

    post "community/index" 

    post "community/search" 

    post "avatar/upload" 

    post "email/remind" 

    resources :blogs do 
    resources :posts 
    end 

    # The priority is based upon order of creation: 
    # first created -> highest priority. 

    # Sample of regular route: 
    # match 'products/:id' => 'catalog#view' 
    # Keep in mind you can assign values other than :controller and :action 

    # Sample of named route: 
    # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase 
    # This route can be invoked with purchase_url(:id => product.id) 

    # Sample resource route (maps HTTP verbs to controller actions automatically): 
    # resources :products 

    # Sample resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

    # Sample resource route with sub-resources: 
    # resources :products do 
    #  resources :comments, :sales 
    #  resource :seller 
    # end 

    # Sample resource route with more complex sub-resources 
    # resources :products do 
    #  resources :comments 
    #  resources :sales do 
    #  get 'recent', :on => :collection 
    #  end 
    # end 

    # Sample resource route within a namespace: 
    # namespace :admin do 
    #  # Directs /admin/products/* to Admin::ProductsController 
    #  # (app/controllers/admin/products_controller.rb) 
    #  resources :products 
    # end 

    # You can have the root of your site routed with "root" 
    # just remember to delete public/index.html. 
    # root :to => 'welcome#index' 

    root :to => 'site#index' 

    # match'', :controller => 'site', :action => 'index', :id => nil 
    resources :user 
    match '/register', :to => 'user#register' 
    match '/about', :to => 'user#about' 
    match '/help', :to => 'user#help' 
    match '/login', :to =>'user#login' 
    match '/edit', :to =>'user#edit' 
    match '/user/welcome_email', :to => 'user#create' 

    #resources :profile 
    #match '/show', :to => 'profile#show' 

    # Install the default route as the lowest priority. 
    match ':controller/:action/:id' 
    match 'profile', :to => 'profile#show', :as => "profile" 
    match 'hub', :to => 'user#index', :as => 'hub' 

    ##Change Route pagina 343 
    # You can have the root of your site routed by hooking up '' 
    # -- just remember to delete public/index.html. 
    match '', :controller => 'site', :action => 'index', :id => nil 
### 

#match'', :controller => 'user', :action => 'about', :id => nil 
#match'', :controller => 'user', :action => 'help', :id => nil 

# See how all your routes lay out with "rake routes" 

# This is a legacy wild controller route that's not recommended for RESTful applications. 
# Note: This route will make all actions in every controller accessible via GET requests. 
# match ':controller(/:action(/:id(.:format)))' 
end 
あなたのコードを削除した後

ApplicationStacktrace

app/views/posts/_post.erb:4:in `_app_views_posts__post_erb__1793130860277527745_40556500' 
app/views/posts/show.html.erb:3:in `_app_views_posts_show_html_erb__2356198700875868089_40643800' 
app/controllers/posts_controller.rb:23:in `show 

答えて

0

posts_urlにリダイレクトしようとしています。
しかし私はあなたのルートでそれが表示されません。また、コマンドラインでrake routesを実行して、ルートが何であるかを確認することもできます。
posts/indexのゲットを追加する必要があると思いますが、多くのルートをクリーンアップするためには、RESTful routingを試してみてください。

+0

完全なコードはhttps://gist.github.com/37400e3e78bbf6628667でこれをマイケルデュラント –

+0

はい私はそれを見て見ます。コマンドラインで 'rake routes'をアプリケーションのルートディレクトリから入力し、出力をポストしてください。 –

+0

私はもっと見ました。投稿はブログでネストされています。したがって、ブログのインデックスページにリダイレクトするか、リダイレクトしてブログを渡すか、投稿のためにroutes.rbに新しい行を追加する必要があります(ネストされたものだけでなくネストされたものも)。あなたは、「コメント」のようなものにポストを変更することを強く検討して、どこかの予約語である問題に取り組むことができます。ルートを見て、コマンドと出力に精通するためには 'rake routes'をお勧めします。 –

0

post.titleの代わりに@post.titleをテンプレートの4行目に使用してください。

関連する問題