破坏Ruby on Rails教程中的微帖

时间:2014-07-25 05:13:33

标签: ruby-on-rails

每个人,我知道已经有一些关于这个话题的询问,但其他答案都没有解决我的问题。我正在努力完成“摧毁微型帖子"本教程的一部分,并在规范测试中得到以下错误:

  

Micropost页面微博破坏,因为正确的用户应删除a   微柱

 Failure/Error: before { visit root_path }
 SyntaxError:
   /Users/rails_projects/sample_app/app/views/shared/_feed_item.html.erb:11: syntax error, unexpected ':', expecting '}'
   ...ethod: :delete, data { confirm: "You sure?" }, title: feed_i...
   ...                               ^
   /Users/rails_projects/sample_app/app/views/shared/_feed_item.html.erb:11: syntax error, unexpected ',', expecting =>
   ... data { confirm: "You sure?" }, title: feed_item.content );@...
   ...                               ^
 # ./app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb___2425863304392027210_70358052260580'
 # ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__3111465487832718441_70358084930700'
 # ./spec/requests/micropost_pages_spec.rb:38:in `block (4 levels) in <top (required)>'

任何人都可以帮我这个吗?我花了最后7个小时试图解决这个问题。无法找到错误。

#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

#microposts_controller.rb:

class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
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


#_feed_item.html.erb:

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


#_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>

0 个答案:

没有答案