Ruby on rails - Hartl教程 - Micropost总是被检测为空白但它们不是

时间:2013-11-26 13:46:05

标签: ruby-on-rails ruby rspec

我已经实现了Hartl关于ruby on rails的教程。我差不多完成了。一切都在工作,我开始改变一些东西,现在我发现了一个我无法理解的错误,关于以前工作的事情。我可能无意中改变了一些但找不到的东西!

当我尝试从我的主页发布微博(带有非空白内容)时,它总是说:

表单包含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 %>

我真的陷入了这个错误,并为自己发疯而不再注意到这个错误。现在我做了很多改动,不能回到以前的保存......

非常感谢,如果你可以帮助我,我是新手上的ruby on rails

编辑

MODEL: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)

2 个答案:

答案 0 :(得分:0)

假设您正在使用依赖于“强参数”的教程版本,则需要更新控制器以明确指出content是允许的参数。顺便说一句,我认为完全按照书面编写完成教程是有帮助的,然后在完成后分支出来。

答案 1 :(得分:0)

好像你正在使用强参数。控制器底部应该有一个名为micropost_params的方法,它将params.require(:micropost).permit ... add:content声明为permit的参数列表。您希望允许:在执行Micropost.new micropost_params

时批量分配内容