Ruby on Rails"无法找到状态ID#34;

时间:2014-04-24 00:41:16

标签: ruby-on-rails ruby ruby-on-rails-4

我正在做这个树屋项目,某种用ruby on rails(http://teamtreehouse.com/library/building-social-features-in-ruby-on-rails-2)构建的社交网络。

我试图让用户无法更新其他用户的现有状态并被卡住了。这是我的statuses_controller.rb:

  class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_status, only: [:show, :edit, :update, :destroy]


  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.order("created_at DESC").to_a
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
  end

  # GET /statuses/new
  def new
    @status = Status.new
  end

  # GET /statuses/1/edit
  def edit
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = current_user.statuses.new(status_params)

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render action: 'show', status: :created, location: @status }
      else
        format.html { render action: 'new' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /statuses/1
  # PATCH/PUT /statuses/1.json
  def update
    @status = current_user.statuses.find(params[:id])
    params[:status].delete(:user_id) if params[:status].has_key?(:user_id)
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_status
      @status = Status.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def status_params
      params.require(:status).permit(:content, :user_id)
    end
end

在测试时(如果用户'登录时应更新当前用户的状态'),我收到以下错误:

1) Error:
StatusesControllerTest#test_should_update_status_for_the_current_user_when_logged_in:
ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?]
app/controllers/statuses_controller.rb:45:in `update'
test/controllers/statuses_controller_test.rb:76:in `block in <class:StatusesControllerTest>'

2) Error:
StatusesControllerTest#test_should_update_status_when_logged_in:
ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?]
app/controllers/statuses_controller.rb:45:in `update'
test/controllers/statuses_controller_test.rb:70:in `block in <class:StatusesControllerTest>'

12 tests, 24 assertions, 0 failures, 2 errors, 0 skips

这是我的statuses_controller_test.rb:

test "should update status when logged in" do
  sign_in users(:gustavo)
  put :update, id: @status, status: { content: @status.content }
  assert_redirected_to status_path(assigns(:status))
end

test "should update status for the current user when logged in" do
  sign_in users(:gustavo)
  put :update, id: @status, status: { content: @status.content, user_id: users(:paul).id }
  assert_redirected_to status_path(assigns(:status))
  assert_equal assigns(:status).user_id, users(:gustavo).id
end

那是我的夹具users.yml:

gustavo:
  first_name: "Gustavo"
  last_name: "Paiva"
  email: "gustavorpaiva@gmail.com"
  profile_name: "grpaiva"

paul:
  first_name: "Paul"
  last_name: "McCartney"
  email: "paulmccartney@gmail.com"
  profile_name: "paulpaul"

我正在使用Rails 4.0.4,而且我已经在treebook的代码上做了一些工具,所以它可以完美地工作(我认为它是用Rails 2.x制作的)。应该是这种情况还是我在这里遗漏了什么?

Ps。:这是我的项目链接,在进行此更改之前http://sheltered-everglades-2797.herokuapp.com/

谢谢!

1 个答案:

答案 0 :(得分:0)

在put方法中尝试@ status.id而不仅仅是@status

test "should update status when logged in" do
  sign_in users(:gustavo)
  put :update, id: @status.id, status: { content: @status.content }
  assert_redirected_to status_path(assigns(:status))
end

如果这不起作用,你可以发布代码,了解在控制器测试中如何构建@status对象?