如何从Hartl的Ruby on Rails教程中扩展微博模型?

时间:2013-11-30 21:20:11

标签: ruby-on-rails database methods

我已经阅读了Michael Hartl的RoR教程,我想扩展微博模型,以允许用户发布包含关键字,价格范围和条件字段的项目。我已经用微博形式的新方法,微博模型,数据库迁移,工厂和控制器替换了他的教程中的“内容”方法。但是,当我尝试加载表单时,出现以下错误:

Showing C:/Sites/rails_projects/sample_app/app/views/shared/_micropost_form.html.erb where line #4 raised:

undefined method `keyword' for #<Micropost:0x54bd7e0>
Extracted source (around line #4):


  <%= form_for(@micropost) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <div class="field">
      <%= f.text_area :keyword, placeholder: "iPhone 5 16gb" %>
    </div>

    <div class="field">


Trace of template inclusion: app/views/static_pages/home.html.erb

Rails.root: C:/Sites/rails_projects/sample_app

_micropost.html.erb代码:

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: micropost.content %>
  <% end %>
</li>

microposts_controller.rb代码:

class MicropostsController < ApplicationController
  before_action :signed_in_user
  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

factories.rb代码:

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

  factory :micropost do
    keyword "iPhone 5"
    price "500"
    condition "used"
    user
  end
end

[timestamp] _create_microposts.rb代码:

class CreateMicroposts < ActiveRecord::Migration
  def change
    create_table :microposts do |t|
      t.string :keyword
      t.integer :price
      t.string :condition
      t.integer :user_id

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

micropost.rb代码:

class Micropost < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order('created_at DESC') }
  validates :keyword, presence: true, length: { maximum: 140 }
  validates :price, presence: true, length: { maximum: 140 }
  validates :condition, presence: true, length: { maximum: 140 }
  validates :user_id, presence: true
end

_micropost_form.html.erb代码:

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :keyword, placeholder: "iPhone 5 16gb" %>
  </div>

  <div class="field">
    <%= f.text_area :price, placeholder: "$350-400" %>
  </div>

   <div class="field">
    <%= f.text_area :condition, placeholder: "Used" %>
  </div>


  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

如何正确定义这些新方法,并使其在每次提交时在一个帖子下保存在数据库中?

2 个答案:

答案 0 :(得分:0)

尝试运行迁移(如果尚未运行)并重新启动服务器

答案 1 :(得分:0)

http://sourceforge.net/projects/sqlitebrowser/

下载,运行该应用程序。打开你的数据库...它将在你的应用程序所在的位置......然后是db / development.sqlite3

点击browse data标签。切换到你的微博。你的新专栏在哪里?可能不是......

我的猜测是你刚进入你的迁移文件并开始修修补补。不,没有。

运行db:migrate后,将构建数据库。您要进行的任何更改都必须以新的迁移或回滚的形式添加。

所以在你的情况下...你可能想要运行rails g migration add_price_to_microposts,这将在db / migrate下创建一个新文件。

class AddPriceToMicroposts < ActiveRecord::Migration
  def change
    add_column :microposts, :keyword, :string
    add_column :microposts, :price, :integer
    add_column :microposts, :condition, :string
  end
end

你的另一个选择是回滚。 rake db:migrate:down VERSION=20131130180735。这会杀了你的桌子......但是你的情况可能还行。现在您可以返回到该迁移文件...以t.sting :whatever的形式进行所需的更改,然后当您满意时,rake db:migrate重建该表。