未定义的方法'每个'对于create方法中的nil类

时间:2015-05-02 23:02:22

标签: ruby-on-rails jointable

我得到一个未定义的方法'每个'我的创建方法中的nil类为我所拥有的连接表。

我为Emotions_pins设置了一个连接表,为casues_pins设置了一个连接表情感引脚表工作正常,但我收到了错误原因。这是代码

_form.html.erb for Pin

<%= form_for(@pin) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>

    <% if @pin.errors.any? %>
     <div id="error_explanation">
     <h2><%= pluralize(@pin.errors.count, "error") %> prohibited this checkin    from being saved:</h2>

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

    <h3>Hi! Thank you for choosing to check-in with your teacher!  This is a  great way to get help, share your feelings and concerns, and make your school a safer place to learn.  </h3>


  <div class="form-group">
  <%= label_tag(:classroom, "Select your classroom:") %>
  <%= select_tag "pin[code]",    options_from_collection_for_select(Classroom.all, "code", "code", ) %>
</div>
 <h4>Where?</h4>
   <% @causes.each do |cause| %>
     <div class="checkbox">
       <ul>
         <li>
           <%= check_box_tag "reflection[cause_ids][]", cause.id %>
           <%= label_tag(cause.name) %>
         </li>
        <ul>
      </div>
   <% end %>

   <div class="form-group">

    <%= image_tag 'feelings.png', class: "image" %>
    <h4>How are you feeling?</h4>
   </div>

  <% @emotions.each do |emotion| %>
     <div class="checkbox">
      <%= check_box_tag "pin[emotion_ids][]", emotion.id %>
      <%= label_tag(emotion.name) %>
     </div>
    <% end %> 

 <div class="form-group">
   <h4> You can <strong>free write </strong> </h4>
   <p> I want my teacher to know _____________________________________.</p>
   <%= f.text_area :question, class: "form-control" %>
  </div>
  <div class="form-group">
   <h4> You can write about your own actions or thoughts here.</h4>
   <p>Something I did was ________________________________.</p>
   <%= f.text_area :question1, class: "form-control" %>
 </div>
  <div class="form-group">
   <h4>You can write about the actions of another person here..</h4>
   <p>Something __(name)_____did was___________________________________.</p>
    <%= f.text_area :question2, class: "form-control" %>
 </div>
 <div class="form-group">
  <h4>Do you have a question for your teacher?</h4>
  <p>I want to ask my teacher ______________________________________.</p>
  <%= f.text_area :question3, class: "form-control" %>
 </div>
 <div class="form-group">
 <h4>Are you thinking about <strong>doing something else</strong>? You can write about it here.</h4>
 <p>Something else I might do is ______________________________________.</p>
   <%= f.text_area :question4, class: "form-control" %>
 </div>
 <div class="form-group">
  <%= f.submit "submit", class: "btn btn-lg btn-primary" %>
 </div>
<% end %>

编辑[ADDED pin_controller.rb]

class PinsController < ApplicationController
 before_action :set_pin, only: [:show, :edit, :update, :destroy]
 respond_to :html    s

def home
   @pins = Pin.all
   respond_with(@pins)
   authorize @pins
end
def show
  respond_with(@pin)
end

def new
  @pin = Pin.new
  @emotions = Emotion.all 
  @causes = Cause.all
  @school = School.find(params[:school])
  respond_with(@pin)
  authorize @pin
 end

 def edit
 end

def create
  code = params[:pin][:code]
  @classroom = Classroom.where('code LIKE ?', code).first
  unless @classroom
    flash[:error] = "Classroom code incorrect"
    @emotions = Emotion.all 
    @causes = Cause.all
  render :new
else
  params[:pin][:classroom_id] = @classroom.id

  @pin = Pin.new(pin_params)
  @pin.save 
  params[:pin][:cause_ids].each do |cause_id| 
   @cause = Cause.find(cause_id)
   @pin.causes << @cause
  end
  params[:pin][:emotion_ids].each do |emotion_id| 
    @emotion = Emotion.find(emotion_id)
    @pin.emotions << @emotion
  end
  if @pin.save
    redirect_to signout_path and return 
  end 
    respond_with(@pin)
    authorize @pin
  end
 end

def update
  @pin.update(pin_params)
  respond_with(@pin)
  authorize @pin
end

def destroy
   @pin.destroy
   respond_with(@pin)
   authorize @pin
end

private
def set_pin
  @pin = Pin.find(params[:id])
  authorize @pin
end

def pin_params
  params.require(:pin).permit(:user_id, :question, :question1, :question2, 
    :question3, :question4, :question5, :classroom_id, :sad, 
    :happy, :mad, :brave, :embarrassed, :sorry, :frustrated, 
    :silly, :left_out, :excited, :hurt, :jealous, :confused, 
    :proud, :other)
  end
end

这是我得到的确切错误

enter image description here

到目前为止,我还是无法弄清楚问题所在。我错过了什么?

1 个答案:

答案 0 :(得分:1)

愚蠢的错误,但直到我发布表单代码才发现它...感谢您的帮助!

cpp : inititial implementation of cppia scripting

应该是

"reflection[cause_ids][]" 

再次感谢您的帮助:)

 "pin[cause_ids][]"