我正在嘗試在推文中添加標題以在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
結束了工作,非常感謝! – user2561901