【问题标题】:rails 4.0 undefined methodrails 4.0 未定义的方法
【发布时间】:2014-05-29 21:11:28
【问题描述】:

我有一个 Opportunity 模型,它具有 Activity 作为嵌套资源。在我的opportunities/show 页面上,我有一个针对该机会的活动列表和一个添加新活动的表单。当我点击“添加活动”时,我得到:

nil:NilClass 的未定义方法 `activities'

这是错误来源:

 # POST /activities.json
  def create
    @activity = @opportunity.activities.new(activity_params)
    if @activity.save
      redirect_to @opportunity, notice: 'Activity has been added'
    else

我将我的Opportunity 模型定义为具有许多Activities 并且我的活动belongs to 是一个机会。以下是我的Activity 控制器的相关部分:

def create
  @activity = @opportunity.activities.new(activity_params)
  if @activity.save
    redirect_to @opportunity, notice: 'Activity has been added'
  else
    redirect_to @opportunity, alert: 'Unable to add Activty'
  end
end

这是我的观点/活动/新代码

<%= form_for ([@opportunity, @opportunity.activities.new]) do |f| %>
<div class="field">
    <%= f.label "Date Assigned" %> <br />
    <%= f.text_field :date_assigned %> 
</div>
<div class="field">
    <%= f.label "Date Due" %> <br />
    <%= f.text_field :date_due %> 
</div>
<div class="field">
    <%= f.label "Description" %> <br />
    <%= f.text_field :description %> 
</div>
<div class="field">
    <%= f.label "Status" %> <br />
    <%= f.text_field :status %> 
</div>
<div class="actions">
    <%= f.submit 'Add' %> 
</div>


<% end %>

我的路线:

resources :opportunities do
   resources :activities
end

谢谢!!

【问题讨论】:

    标签: ruby-on-rails methods undefined


    【解决方案1】:

    您的@opportunity 在块中是未定义(无)。

    您必须先获得@opportunity,然后才能在其上进行活动:

    @opportunity = Opportunity.find(params[:opportunity_id])
    

    (:opportunity_id原因:由于这是ActivityController,并且您的模型是嵌套的,因此通过常规嵌套的 RESTful 资源(如您的路线中指定的那样),参数自动分配为 model_id => opportunity_id)

    更改代码:

    def create
      @opportunity = Opportunity.find(params[:opportunity_id])
      @activity = @opportunity.activities.new(activity_params)
      if @activity.save
        redirect_to @opportunity, notice: 'Activity has been added'
      else
    

    另外,在为关系构建对象时,使用build 而不是newrecommend

    【讨论】:

    • 只是好奇,你为什么推荐build而不是new?
    • 虽然,从 cmets 线程和进一步探索来看,new 现在的行为与自 Rails 3.2.13 以来的 build 相同
    • 这是一个旧答案,这在 Rails 3 的某些版本中有所改变。build 现在只是new 的别名github.com/rails/rails/blob/4-0-6/activerecord/lib/…
    • @Iceman 是的,我也看到了。感谢您的提示 :) 不过,我仍然更喜欢在我的项目中使用 build 而不是 new;在我处理关系对象或直接处理模型时提供直观性;我个人的电话;)。很高兴有其他选择。
    【解决方案2】:

    尝试使用 build 而不是 new。

    @activities = @oportunities.activities.build(activity_params)
    

    应该可以的

    编辑:

    在构建之前你没有找到@oportunities :P

    def create   
      @oportunities.find(params[:id])
      @activity = @opportunity.activities.new(activity_params)
      if @activity.save
        redirect_to @opportunity, notice: 'Activity has been added'
      else
        redirect_to @opportunity, alert: 'Unable to add Activty'
      end
    end
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2013-02-05
    相关资源
    最近更新 更多