2013-12-17 2 views
0

레일 4에 has_manypolymorphic 연관을 사용할 방법이 있습니까? 다음은 레일 4 다형성 연관 has_many

booth 모델

다음
has_many :chat, as: :chattable, dependent: :destroy 
    accepts_nested_attributes_for :chat 

입니다 내가 해봤

chat 모델

class Chat < ActiveRecord::Base 
    belongs_to :from_user, class_name: 'User' 
    belongs_to :to_user, class_name: 'User' 
    belongs_to :chattable, polymorphic: true 
end 

내 부스 컨트롤러 chattable 및 뷰를 검색 할 수없는 것입니다

@booth.chat 

하지만, 그것은 정의되지 않은 방법을 말한다

부스 컨트롤러 내가 볼 수있는 유일한 문제는 당신 booth 모델에

class BoothsController < ApplicationController 
    load_and_authorize_resource 
    before_filter :authenticate_user! 
    before_action :set_booth, only: [:show, :edit, :update, :destroy, :about, :products, :literature, :partners, :presentations, :contact, :videos] 
    before_action :sidebar_menu 

    layout "users" 

    def index 
     @booths = Booth.all 
    end 

    def show 
     @booth.build_chats 
     hide_sidebar 'hide-sidebar' 
     @menu_items << [edit_booth_path(@booth), "Edit this booth"] 
     add_sidebar_menu ["Directory", "home"], "home-nav", 
      [[hall_visit_path(@booth.hall), "Visit hall"]], false 
     respond_to do |format| 
      format.html 
      format.js 
     end 
    end 

    def new 
     @booth = Booth.new 
     @booth.build_uploaded_file 
    end 

    def edit 
     @menu_items << [booth_path(@booth), "Show this booth"] 
     @booth.build_uploaded_file unless @booth.uploaded_file 
    end 

    def create 
     @booth = Booth.new(booth_params) 

     respond_to do |format| 
      if @booth.save 
      format.html { redirect_to @booth, notice: 'Booth was successfully created.' } 
      format.json { render action: 'show', status: :created, location: @booth } 
      else 
      format.html { render action: 'new' } 
      format.json { render json: @booth.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

    def update 
     if params[:booth][:uploaded_file_attributes][:assets].blank? 
      params[:booth].delete(:uploaded_file_attributes) 
     end 
     respond_to do |format| 
      if @booth.update(booth_params) 
      sync_update @booth 
      format.html { redirect_to @booth, notice: 'Booth was successfully updated.' } 
      format.json { head :no_content } 
      else 
      format.html { render action: 'edit' } 
      format.json { render json: @booth.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

     def destroy 
     @booth.destroy 
     respond_to do |format| 
      format.html { redirect_to booths_url } 
      format.json { head :no_content } 
     end 
     end 


    def about 
     render layout: false 
    end 

    def products 
     render layout: false 
    end 

    def literature 
     render layout: false 
    end 

    def partners 
     render layout: false 
    end 

    def presentations 
     render layout: false 
    end 

    def contact 
     render layout: false 
    end 

    def videos 
     render layout: false 
    end 

    def chat_widget 

    end 

     private 
     # Use callbacks to share common setup or constraints between actions. 
     def set_booth 
      @booth = Booth.find(params[:id]) 
      uploaded_file_url(@booth) 
     end 

     # Never trust parameters from the scary internet, only allow the white list through. 
     def booth_params 
      params.require(:booth).permit(:name, :company_website, :social_media, :contact_info, :email, :about_us, :greeting_type, :event_id, :public_chat, :twitter_roll, :twitter_hash_tag, :survey_url, :prize_giveaway_description, :newsletter_description, :greeting_image_id, :greeting_audio_id, :greeting_video_id, :greeting_virtual_id, :user_id, :booth_package, :display_mode, :greeting_video, 
      :facebook_url, :linkedin_url, :twitter_url, :top_message, :template_id, :hall_id, uploaded_file_attributes: [:assets]) 
     end 

     def uploaded_file_url(booth) 
      @booth_logo_url = booth.uploaded_file.assets.url if booth.uploaded_file 
     end 

     def sidebar_menu 
      super ["Booths", "suitcase"], "booth-nav", [[booths_path, "List booths"], [new_booth_path, "Create a booth"]], true 
     end 
end 

답변

2

:

has_many :chat, as: :chattable, dependent: :destroy 

것은 당신이 has_many를 사용할 때 복수 chats을 사용해야합니다 . 로 그 라인을 교체하십시오 : @booth.chats :

has_many :chats, as: :chattable, dependent: :destroy 

는 그런 다음에 의해 부스의 채팅을 참조 할 것.

+0

감사합니다. 부스 컨트롤러에서도 @buoth.build_chats를 실행해야합니까? –

+0

또한 내가하려고 할 때'= sync partial : 'chat_widget', resource : @ booth.chats'을 #하려고합니다. # 에 대해'undefined method'model_name '을 얻습니다. @ –

+0

@PassionateDeveloper, 네 협회 설립을 위해 그렇게해야합니다. 나는이 글을 쓰는 동안 다른 의견을 보았으므로 컨트롤러 코드도 올리시기 바랍니다. 문제가 컨트롤러에 없으면 양식 코드를 살펴 봐야 할 수도 있습니다. – vee