简体   繁体   English

Rails未定义的方法*用于nil:NilClass

[英]Rails undefined method * for nil:NilClass

I am having an issue and I have done some reasearch, from my research I have found that the variable that is being used is empty, however I am unsure as to why it is empty, Maybe its something obvious to someone else? 我遇到了一个问题,并且进行了一些研究,从研究中我发现所使用的变量为空,但是我不确定为什么它为空,也许对其他人很明显?

I am trying to display a nested form on a page from another controller, I am used nested resources, Which I think might be my issue, but unsure how to resolve it. 我试图在另一个控制器的页面上显示嵌套表单,我使用的是嵌套资源,我认为这可能是我的问题,但不确定如何解决。

Getting the following error: 出现以下错误:

undefined method `submission' for nil:NilClass

Structure of Project Main Folders -Members --Questions --Submissions 项目主文件夹的结构-成员-问题-提交

Concept: 概念:

Question - has_many - Submissions Submission - Belongs_to - Question 问题-has_many-提交内容-属于-问题

Submission Model: 提交模型:

class Submission < ActiveRecord::Base
    belongs_to :question
    belongs_to :member
end

Question Model: 问题模型:

class Members::Question < ActiveRecord::Base
  has_many :submissions
end

Submissions Controller: 提交负责人:

def create

        @question =  Members::Question.find(params[:question_id])
        @submission.member_id =  current_member.id
        @submission = @question.submissions.create(params[:submission].permit(:content, :question_id))

*Redirect Method Here * 

end

In the form I am using the following method: 在表单中,我使用以下方法:

<%= form_for([@question, @question.submission.build]) do |f| %>
  <% if @submission.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@submission.errors.count, "error") %> prohibited this submission from being saved:</h2>

      <ul>
      <% @submission.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And then to display the form on the show page of the question, I am using 然后在问题的显示页面上显示表单,我正在使用

<%= render 'members/submissions/form' %>

Routes: 路线:

  namespace :members do
    resources :questions,  only: [:index,:show] do
      resources :submissions 
    end
  end

Any one any ideas where I am going wrong? 任何人有任何想法我要去哪里错吗?

Any help is appreciated. 任何帮助表示赞赏。

I have solved the problem, Thank you for the suggestions, I was using the wrong variable, I was using @question, When because its nested, the correct variable is @members_question 我已经解决了问题,谢谢您的建议,我使用的是错误的变量,我使用的是@question,由于嵌套,正确的变量是@members_question

Submissions Controller 提交控制器

def create

        @members_question =  Members::Question.find(params[:question_id])
        @submission = @members_question.submissions.create(params[:submission].permit(:content, :question_id))
    @submission.member_id =  current_member.id

end

_form.html.erb _form.html.erb

<%= form_for([@members_question, @members_question.submissions.build]) do |f| %>

  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM