【问题标题】:Why is instance variable nil in my rails partial?为什么我的 Rails 中的实例变量 nil 不完整?
【发布时间】:2013-05-07 12:07:52
【问题描述】:

我的应用中有一个可以创建帖子的用户模型(代理)。他们可以从仪表板执行此操作。我还在他们的仪表板中显示了他们所有现有的帖子。当试图创建一个没有我正在测试以确保它呈现错误的内容的新帖子时,问题就出现了。创建一个包含内容的帖子是可行的,但创建一个不包含内容的帖子会不断触发一个错误,提示我的@posts 实例变量为零。不确定我是否遗漏了什么?

仪表板视图:

.container
    .column.span9

      - if current_agent
        = render 'home/shared/agent_post_panel'
        = render 'home/shared/agent_dashboard_tabs'

agent_dashboard_tabs:

  .tabs-container
    #posts
      .content
        - if @posts.any? //This is where the error is triggered
          = render partial: 'shared/post', collection: @posts

仪表板控制器:

class HomeController < ApplicationController
  before_filter :authenticate!, only: [:dashboard]

  def index
  end

  def dashboard
    if current_agent
      @post  = current_agent.posts.build
      @posts = current_agent.posts
    end
  end
end

我的后期控制器:

class PostsController < ApplicationController
  before_filter :authenticate_agent!


  def create
    @post = current_agent.posts.build(params[:post])
    if @post.save
      flash[:notice] = "Post created!"
      redirect_to dashboard_path
    else
      flash[:error] = "Post not created"
      render 'home/dashboard'
    end
  end

end

测试:

feature 'Creating posts' do

  let(:agent) { FactoryGirl.create(:agent) }

  before do
    sign_in_as!(agent)
    visit dashboard_path
  end

  scenario "creating a post with valid content" do
    fill_in 'post_content', :with => 'I love donuts'
    expect { click_button "Post" }.to change(Post, :count).by(1)
  end

  scenario "creating a post with invalid content" do
    expect { click_button "Post" }.not_to change(Post, :count)
    click_button "Post"
    page.should have_content("Post not created")
    page.should have_content("can't be blank")
  end

【问题讨论】:

  • +1 用于测试的综合问题 :)

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

当帖子出现错误时,您正在从posts#create 中渲染home/dashboard,但您没有设置@posts 变量。

你应该在render 'home/dashboard'之前添加这个

@posts = current_agent.posts

【讨论】:

  • 工作就像一个魅力,完全忽略了这一点。干杯
【解决方案2】:

使用redirect_to dashboard_path 代替render 'home/dashboard'。 并且您可以通过在home#dashboard 中调用keep 的flash 方法将flash 消息保留在仪表板上。

flash.keep

【讨论】:

    【解决方案3】:

    如果我正确理解您的问题,您在加载页面时遇到问题 (HomeController#index)

    正如我在加载索引操作之前看到的那样,您检查用户是否已登录,

    确保您的应用程序流进入

    if current_agent
          #make sure you application flow comes here
          @post  = current_agent.posts.build
          @posts = current_agent.posts
    end
    

    如果不是,您可能需要稍微修改此流程(如果这符合您的要求)

    if current_agent
              @post  = current_agent.posts.build
              @posts = current_agent.posts
    else
        @post = Post.new
        @posts = []
    end
    

    最后,当你不确定你得到的对象时,使用try 总是好的

    if @posts.try(:any?)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多