2016-06-29 5 views
0

どうすれば解決できますか?適切に切り替えるにはしたくない 二link_to link_toでインスタンス変数をproprerly使用する方法はありますか?

- if subject.participation(current_participant).nil? 
    = link_to "Ca m'intéresse !", subject_participant_index_path(:interested_id => current_participant.id, :subject_id => subject.id), remote: true, :method => :post, class:"btn btn-primary" 

    - else 
    = link_to "Ca ne m'intéresse plus !", delete_participation_path(@subject.participation(current_participant).id),:method => :delete, remote: true, class:"btn btn-primary" 

/views/subjects/_inscription_button.html.haml:

は、私は2つのボタンを持っていました。私はエラーを取得する:/ subject_participant/115 未定義のメソッドはnilのための `ID」で

NoMethodError:NilClass

インスタンス変数subject最初のボタンのための作品ではなく、二...

ここで

は便利なコードの残りの部分である: subject_participant_controller.rb:

class SubjectParticipantController < ApplicationController 
    before_action :authenticate_participant! 


    def create 
    @subject = Subject.find(params[:subject_id]) 
    @subject_participant = SubjectParticipant.new(subject_participant_params) 

    if @subject_participant.save 
    respond_to do | format | 
     format.html {redirect_to subjects_path} 
     format.js 
    end 
    else 
    redirect_to subjects_path 
    end 
    end 

    def destroy 
    @subject_participant = SubjectParticipant.find(params[:id]) 
    if @subject_participant.destroy 
    respond_to do | format | 
     format.html {redirect_to subjects_path} 
     format.js 
    end 
    else 
    redirect_to subjects_path 
    end 
    end 

    def subject_participant_params 
    params.permit(:interested_id, :subject_id, :id) 
    end 
end 

/routes.rb:対象の代わりに

Rails.application.routes.draw do 
devise_for :participants 
resources :subjects 
resources :participants 
resources :conferences 
resources :subject_participant 

delete 'subject_participant/:id' => 'subject_participant#destroy', as: 'delete_participation' 
root 'welcome#index' 

subject.rb

class Subject < ActiveRecord::Base 
    validates_presence_of :title, :questioner, :conference, :description 

    has_many :subject_participants 
    has_many :interested, through: :subject_participants #interested 
    belongs_to :questioner, class_name: "Participant" 
    belongs_to :conference 

    def participation(current_participant) 
    self.subject_participants.find_by_interested_id(current_participant.id) 
end 
end 

答えて

0

ただ、ユーザー@subject。

- if subject.participation(current_participant).nil? 
= link_to "Ca m'intéresse !",subject_participant_index_path(:interested_id => current_participant.id, :subject_id => @subject.id), remote: true, :method => :post, class:"btn btn-primary" 

- else 
= link_to "Ca ne m'intéresse plus !", delete_participation_path(@subject.participation(current_participant).id),:method => :delete, remote: true, class:"btn btn-primary" 
+0

これは、nilに対して他のエラー 'undefined method 'participation'を送信します:NilClass' – Orsay

1

@subject.participation(current_participant)はゼロです。あなたはそれを修正する必要があります。

Railsビューでインスタンス変数を使用する場合は、対応するコントローラアクションでインスタンス変数を定義する必要があることに注意してください。あなたの部分ビューから親ビューに戻る。どのコントローラーアクションに結びついていますか?

ここで、部分的に必要なインスタンス変数を定義します。

+0

はい、このメソッドはビューに存在する 'subject'変数にアクセスできないため、これはnilです。これは実際に私の問題です – Orsay

+0

'@ subject.participation(current_participant)'に 'id'を呼んでいます。 '@ subject.participation(current_participant)'はnilであり、それがあなたの問題です。そのことを念頭に置いてアプリケーションのデバッグを開始する必要があります。 – Tass

関連する問題