nil的未定义方法'title':NilClass

时间:2012-03-08 17:44:54

标签: ruby ruby-on-rails-3 ruby-on-rails-3.1

我正在关注railsspace book但是当我尝试编辑show并消除帖子所有者时(第15章)我得到:错误消息

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) %> |

Ruby 1.9.2 Rails 3.1.3

控制器,视图和路线如下:

Controller: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>

在这个视图中,我在变量post = @ post中进行了@Ting,但是它不起作用 路由

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

2 个答案:

答案 0 :(得分:0)

删除代码后,我们会尝试重定向到posts_url 但是我在你的路线中没有看到。顺便说一句,您也可以在命令行中rake routes查看您的路线 我认为您需要为posts/index添加一个获取,但我真的希望尝试使用RESTful routing来清理您的大部分路线。

答案 1 :(得分:0)

在模板的第4行使用@post.title代替post.title