![](/img/trans.png)
[英]How can I expand the micropost model from Hartl's Ruby on Rails tutorial?
[英]Ruby on rails - Hartl tutorial - Micropost always detected as blank but they are NOT
我已经了解了Hartl的关于红宝石的教程。 我快完成了。 一切都正常了,我开始更改一些东西,但是现在我对一个以前可以正常工作的东西有一个我无法理解的错误。 我可能无意间更改了某些内容,但找不到任何内容!
当我尝试从主页发布微博(内容不空白)时,它总是说:
该表格包含1个错误。 *内容不能为空
我试图启动$ bundle exec rspec spec /,并且出现了一个新错误:
8) Micropost pages micropost creation with valid information should create a micropost
Failure/Error: expect { click_button "Post" }.to change(Micropost, :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/requests/micropost_pages_spec.rb:29:in `block (4 levels) in <top (required)>'
所以我认为这是相关的,但是,我不太了解错误是什么,所以我不知道如何解决!
micropost_pages_spec.rb
require 'spec_helper'
describe "Micropost pages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "micropost creation" do
before { visit root_path }
describe "with invalid information" do
it "should not create a micropost" do
expect { click_button "Post" }.not_to change(Micropost, :count)
end
describe "error messages" do
before { click_button "Post" }
it { should have_content('error') }
end
end
describe "with valid information" do
before { fill_in 'micropost_content', with: "Lorem ipsum" }
it "should create a micropost" do
expect { click_button "Post" }.to change(Micropost, :count).by(1)
end
end
end
describe "micropost destruction" do
before { FactoryGirl.create(:micropost, user: user) }
describe "as correct user" do
before { visit root_path }
it "should delete a micropost" do
expect { click_link "delete" }.to change(Micropost, :count).by(-1)
end
end
end
end
micropost_spec.rb
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum") }
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
its(:user) { should eq user }
it { should be_valid }
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
describe "with blank content" do
before { @micropost.content = " " }
it { should_not be_valid }
end
describe "with content that is too long" do
before { @micropost.content = "a" * 141 }
it { should_not be_valid }
end
end
_micropost_from.html.erb
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
我真的被这个错误所困扰,并为自己之前没有注意到这个错误而生气。 现在我做了很多更改,无法返回到以前的保存...
非常感谢您能为我提供帮助,我是红宝石新手
编辑中
型号:micropost.rb
class Micropost < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end
服务器日志
Started POST "/microposts" for 127.0.0.1 at 2013-11-26 15:00:07 +0100
Processing by MicropostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eSMhNrhM50lg8rDcz0G6OYWW6/ssGmH+PZja2ghjJzA=", "micropost"=>{"content"=>"Try to post"}, "commit"=>"Post"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = '0b245e78962e34d49d4fbb6d2e5abc804f9581e9' LIMIT 1[0m
Unpermitted parameters: content
[1m[35m (0.1ms)[0m begin transaction
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = ? [["user_id", 102]]
Rendered shared/_user_info.html.erb (1.3ms)
Rendered shared/_error_messages.html.erb (0.5ms)
Rendered shared/_micropost_form.html.erb (1.7ms)
Rendered shared/_feed.html.erb (0.0ms)
Rendered static_pages/home.html.erb within layouts/application (4.9ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 20ms (Views: 16.2ms | ActiveRecord: 0.4ms)
假设您使用的教程版本依赖于“强参数”,则需要更新控制器以明确表明content
是允许的参数。 顺便说一句,我认为完全按照书面要求完成本教程,然后在完成后进行分支会有所帮助。
似乎您正在使用强参数。 应该有一个名为在控制器的声明底部micropost_params方法params.require(:micropost).permit ...
添加:内容为参数列表permit
。 您想要允许:content在执行Micropost.new micropost_params
时被大量分配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.