【问题标题】:How can I proprerly use an instance variable in a link_to?如何在 link_to 中正确使用实例变量?
【发布时间】:2016-06-29 14:31:13
【问题描述】:

我该如何解决这个问题?

我有 2 个按钮: /views/subjects/_inscription_button.html.haml

  - 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"

第二个link_to 不想正常切​​换。我得到错误:

/subject_participant/115 处的 NoMethodError nil:NilClass 的未定义方法“id”

实例变量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'

主题.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

【问题讨论】:

    标签: ruby-on-rails ajax link-to


    【解决方案1】:

    它告诉你@subject.participation(current_participant) 是零。你需要修复它。

    请记住,在 Rails 视图中使用实例变量时,需要在相应的控制器操作中定义它们。从您的局部视图后退到其父视图。它与哪个控制器操作相关联?

    您将在这里定义部分所需的实例变量。

    【讨论】:

    • 是的,它是 nil,因为该方法无法访问视图中存在的变量 subject。这其实是我的问题
    • 您在@subject.participation(current_participant) 上致电id@subject.participation(current_participant) 为零,这是你的问题。考虑到这一点,您需要开始调试您的应用程序。
    【解决方案2】:

    只需用户 @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"
    

    【讨论】:

    • 它向我发送了另一个错误undefined method 'participation' for nil:NilClass
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2018-05-31
    • 2022-11-13
    • 2021-06-22
    相关资源
    最近更新 更多