[英]How can I expand the micropost model from Hartl's Ruby on Rails tutorial?
I've gone through Michael Hartl's RoR tutorial, and I'd like to expand the micropost model to allow a user to post an item that contains a keyword, price range, and condition field. 我已经完成了Michael Hartl的RoR教程,并且希望扩展微博模型,以允许用户发布包含关键字,价格范围和条件字段的商品。 I've replaced the "content" method in his tutorial with these new methods in the micropost form, micropost model, db migration, factories, and controller.
我已经用微博表单,微博模型,数据库迁移,工厂和控制器中的这些新方法替换了他教程中的“内容”方法。 However, when I try to load the form, I get the following error:
但是,当我尝试加载表单时,出现以下错误:
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 code: _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 code: 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 code: 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 code: [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 code: 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 code: _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 %>
How can I correctly define these new methods, and make it so that the three attributes are saved in the db under one post every time they're submitted? 我该如何正确定义这些新方法,并使其实现,以便每次提交时将这三个属性保存在数据库中的一则帖子中?
尝试运行迁移(如果尚未运行),然后重新启动服务器
http://sourceforge.net/projects/sqlitebrowser/ http://sourceforge.net/projects/sqlitebrowser/
download that, run that application. 下载该文件,然后运行该应用程序。 open your db... it'll be wherever your app is... then db/development.sqlite3
打开您的数据库...无论您的应用程序在哪里...然后db / development.sqlite3
click on the browse data
tab. 单击
browse data
选项卡。 switch to your microposts. 切换到您的微博。 are your new columns in there?
您的新专栏在那里吗? probably not...
可能不是...
my guess is you just went into your migration files and started tinkering. 我的猜测是您只是进入迁移文件并开始修补。 no no.
不,不。
once you run a db:migrate, your database is built. 一旦运行db:migrate,就将建立数据库。 any changes that you want to make have to be added in the form of a new migration or rollback.
您要进行的任何更改都必须以新的迁移或回滚的形式添加。
so in your case... you'll probably want to to run rails g migration add_price_to_microposts
, which will create a new file under 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
your other option is to rollback. 您的另一个选择是回滚。
rake db:migrate:down VERSION=20131130180735
. rake db:migrate:down VERSION=20131130180735
。 this will kill your table... but that's probably ok in your case. 这会杀死您的桌子...但是您的情况可能没问题。 now you can go back into that migration file... make the changes that you want in the form of
t.sting :whatever
, then when you're content, rake db:migrate
to rebuild the table. 现在您可以回到该迁移文件...以
t.sting :whatever
的形式进行所需的更改,然后在满意时使用rake db:migrate
重建表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.