简体   繁体   English

通过Rails中的两个不同的控制器创建联接表记录

[英]Creating a join table record through two different controllers in rails

I'm trying to get my head around the best way to add a record to a join table through alternative controllers in rails. 我试图通过最好的方法通过rails中的备用控制器将记录添加到联接表中。

I have various models in my app that will require this, but I'm focusing on these two first before I transcribe the method into others, so shall use this as the example. 我的应用程序中有多种模型需要使用此模型,但是在将方法转换为其他方法之前,我首先关注这两个模型,因此将其用作示例。 I have a Venue and Interest model which are to be connected through VenuesInterests model (it has a couple of extra optional attributes so isn't a HABTM relationship). 我有一个通过VenuesInterests模型连接的Venue and Interest模型(它具有几个额外的可选属性,所以不是HABTM关系)。 A user can admin a Venue instance and/or an Interest instance and therefore there should be an ability to select Venues to attach to an Interest and likewise Interests to attach to a Venue. 用户可以管理场所实例和/或兴趣实例,因此应该能够选择要附加到兴趣的场所,并且同样可以选择要附加到场所的兴趣。 This should be done with an Add Venues link on the Interest instance view and an Add Interests link on the Venue instance view. 这应通过“兴趣”实例视图上的“添加场地”链接和“场地”实例视图上的“添加兴趣”链接来完成。 This would then take the user to a list of the relevant instances for them to select ones they would like to select. 然后,这会将用户带到相关实例的列表,供他们选择要选择的实例。

Here are my models: 这是我的模型:

Venue.rb Venue.rb

class Venue < ActiveRecord::Base


has_many :interests, through: :venue_interests
    has_many :venues_interests, dependent: :destroy

    accepts_nested_attributes_for :venues_interests, :allow_destroy => true

end

Interest.rb Interest.rb

class Interest < ActiveRecord::Base

has_many :venues, through: :venue_interests
    has_many :venues_interests, dependent: :destroy

end

VenuesInterests.rb VenuesInterests.rb

class VenuesInterest < ActiveRecord::Base

    belongs_to :interest
belongs_to :venue
validates :interest_id, presence: true
validates :venue_id, presence: true

end

This all seems fine, however it's the controller and views that I'm struggling with. 一切似乎都很好,但是我正在努力使用的是控制器和视图。 I've tried adding an extra method add_interest to the Venues controller to do the job of the create method in the VenuesInterests controller, so that there will be a different view when adding Venues to an Interest than there would be adding Interests to a Venue, otherwise I don't know how I would do this. 我尝试过在Venues控制器中添加额外的方法add_interest来完成VenuesInterests控制器中create方法的工作,因此,将Venues添加到Interest中时会出现与将Venues添加到Venue中时不同的视图,否则我不知道该怎么做。 My current Venues controller is as follows: 我当前的Venues控制器如下:

VenuesController.rb: VenuesController.rb:

class VenuesController < ApplicationController
before_filter :authenticate_knocker!, only: [:new, :edit, :create, :update, :destroy]

respond_to :html, :json

def index
    @venues = Venue.all.paginate(page: params[:page]).order('created_at DESC')
end

def show
    @venue = Venue.find(params[:id])
    @hash = Gmaps4rails.build_markers(@venue) do |venue, marker|
        marker.lat venue.latitude
        marker.lng venue.longitude
        marker.infowindow venue.name
    end
end

def new
    @venue = Venue.new
end

def edit
    @venue = Venue.find(params[:id])
end

def create
    @venue = current_knocker.venues.create(venue_params)
    respond_to do |format|
        if @venue.save!
            format.html { redirect_to @venue, notice: 'Venue was successfully created.' }
            format.json { render json: @venue, status: :created, location: @venue }
        else
            format.html { render action: "new" }
            format.json { render json: @venue.errors, status: :unprocessable_entity }
        end
    end
end

def update
    @venue = Venue.find(params[:id])
    @venue.update_attributes(venue_params)
    respond_to do |format|
        if @venue.update_attributes(venue_params)
            format.html { redirect_to(@venue, :notice => 'Your Venue was successfully updated.') }
            format.json { respond_with_bip(@venue) }
        else
            format.html { render :action => "edit" }
            format.json { respond_with_bip(@venue) }
        end
    end
end

def destroy
end

def add_interests
    @venues_interests = VenuesInterest.new
    @interests = Interests.all.paginate(page: params[:page]).order(:name)
end

private

def venue_params
    params.require(:venue).permit(:admin... etc)
end

end

This isn't currently working as I'm not sure how to reference other classes within a controller, but the important thing I'd like to know is is there a better way to do this or am I (kind of) on the right track? 由于我不确定如何在控制器中引用其他类,因此该方法目前无法正常工作,但是我想知道的重要一点是,有一种更好的方法可以做到这一点,或者我(是否)在右边跟踪? If anyone has a good method (perhaps a jQuery plugin) for allowing multiple selection of instances for the view, that would be great too! 如果有人有一个很好的方法(也许是一个jQuery插件)来允许为视图选择多个实例,那也将很棒!

In my opinion, I would take advantage of the existing update method to add the relationship between Interest and Venue. 我认为,我将利用现有的update方法来添加“兴趣”和“地点”之间的关系。 I can do like this: 我可以这样:

def update
    @venue = Venue.find(params[:id])
    @venue.update_attributes(params[:venue_params])
    if params[:interest_ids].present?
       @venue.interests = Interest.where(id: params[:interest_ids])
       @venue.save
    end
   #more code to handle the rendering
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM