2015-05-02 2 views
0

Я получаю неопределенный метод «каждый» для класса nil в моем методе create для таблицы соединений, которая у меня есть.Неопределенный метод «каждый» для класса nil в методе create

У меня есть одна таблица соединений для Emotions_pins и одна для casues_pins, таблица эмоций контактов отлично работает, но я получаю ошибку по причинам. Вот код

_form.html.erb для 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 %> 

EDIT [ДОБАВЛЕНО 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

Это будет трудно для любого, чтобы помочь вам в этом, пока мы не можем понять, что такое 'Params [: pin] [: cause_ids] ', потому что это параметр, равный нулю. Вам нужно разместить весь контент файла pins_controller.rb. Это также поможет, если вы разместите содержимое формы, которая отправляет данные методу create – vinhboy

+0

Ok thanks @vinboy Я добавил как форму, так и весь контроллер – user3787971

ответ

1

Глупые ошибки, но не поймали его, пока я не разместил код формы ... Спасибо за помощь!

"reflection[cause_ids][]" 

должен быть

"pin[cause_ids][]" 

Еще раз спасибо за помощь :)

<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 %> 
Смежные вопросы