繁体   English   中英

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

[英]How can I expand the micropost model from Hartl's Ruby on Rails tutorial?

我已经完成了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

factory.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 %>

我该如何正确定义这些新方法,并使其实现,以便每次提交时将这三个属性保存在数据库中的一则帖子中?

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

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重建表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM