2013-08-02 61 views
1

我正在嘗試在推文中添加標題以在Rails中獲得一些練習。我不斷收到地方鳴叫是由頁面上此錯誤:在Michael Hartl的Rails教程中爲推文添加標題

undefined method `title' 

它強調app/views/shared/_micropost_form.html.erb文件的行4:

<%= form_for(@micropost) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field"> 
    <%= f.text_area :title, placholder: "Event name" %> 
    <%= f.text_area :content, placeholder: "Compose new micropost..." %> 
    </div> 
    <%= f.submit "Post", class: "btn btn-large btn-primary" %> 
<% end %> 

這裏是我的microposts_controller.rb文件:

class MicropostsController < ApplicationController 
    before_action :signed_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: :destroy 

    def create 
    @micropost = current_user.microposts.build(micropost_params) 
    if @micropost.save 
     flash[:success] = "Micropost created!" 
     redirect_to root_url 
    else 
     @feed_items = [] 
     render 'static_pages/home' 
    end 
    end 

    def destroy 
    @micropost.destroy 
    redirect_to root_url 
    end 

    private 

    def micropost_params 
     params.require(:micropost).permit(:content) 
    end 

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

和我的[ts]_create_microposts.rb文件:

class CreateMicroposts < ActiveRecord::Migration 
    def change 
    create_table :microposts do |t| 
     t.string :content 
     t.integer :user_id 
     t.string :title 

     t.timestamps 
    end 
    add_index :microposts, [:user_id, :created_at] 
    end 
end 

回答

1

好像你可能已經忘了做遷移:

rails generate migration AddTitleToMicroposts title:string

也可能是db:migrate。本着

此外,您還拼錯佔位4.

+0

結束了工作,非常感謝! – user2561901

0

您在app/views/shared/_micropost_form.html.erb文件拼寫錯誤的佔位符

見行:<%= f.text_area :title, placholder: "Event name" %>

一旦你更正爲佔位符都將工作

相關問題