Ruby on Rails,创建Has_and_belong_to_many对象

时间:2016-01-08 00:18:22

标签: ruby-on-rails ruby

我有以下表格:帐户,地址和联系人

我希望帐户有多个地址,联系人可以有多个地址。

首先,我在地址中为Account和contact创建了foreign_keys,但最终在create表单中有两个字段,因此我将关联更改为Has_and_belongs_to_many并创建了联合表accounts_addresses和contacts_addresses。但现在我失去了从他们的观点创建新帐户和联系地址的方法。有人能帮助我吗?

如何在创建new_address的同时创建与帐户或地址的关联?

我想在帐户和地址的地址上使用new_address和edit_address视图,但我不知道如何配置控制器以接收帐户或地址ID并同时创建地址和关联。

我有new_address链接,将帐户ID传递给新控制器:

<%= link_to new_address_path(:account_id => @accounts.id) do %>
<%= (image_tag("address_new.png", :width => "70%")) %>
<br /> New Address
<% end %>

但是在控制器中我完全迷失了:     class AddressesController&lt; ApplicationController的     before_action:logged_in_user

def address_params
    params.require(:address).permit(:id, :no, :street, :suburb, :code, :details , :city, :state, :country, :account, :contact)
end

def new
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New")

        @addresses = @account.addresses.new(:account_id => params[:account_id])
        @addresses = @contact.addresses.new(:contact_id => params[:contact_id])
    else
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New !Access Denied! ")
    flash[:notice] = "You don't have access to create Addresss."
    redirect_to session.delete(:return_to)
    end
end

def create
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create", details: address_params)
        @address = Address.new(address_params)
        if @address.save
        flash[:notice] = 'Address Saved'
        redirect_to session.delete(:return_to)      
        else
        render "new"
        end
    else
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create !Access Denied! ")
    flash[:notice] = "You don't have access to create Addresss."
    redirect_to session.delete(:return_to)
    end
end


def edit
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit", details: params[:id])
        @addresss = Address.find(params[:id])
    else
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit !Access Denied! ", details: params[:id])
    flash[:notice] = "You don't have access to edit Addresss."
    redirect_to session.delete(:return_to)
    end
end

def update
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Update", details: address_params)
        @addresss = Address.find(params[:id])
        if @addresss.update_attributes(address_params)
        redirect_to session.delete(:return_to)
            flash[:notice] = 'Account Updated'
        else 
        render "edit"
        flash[:error]
        end
    else
    flash[:notice] = "You don't have access to edit Addresss."
    redirect_to session.delete(:return_to)
    end
end

def show
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canread?)
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View", details: params[:id])
        @addresses = @account.addresses.find(:account => params[:account_id])
        @addresses = @contact.addresses.find(:contact => params[:contact_id])
    else
    @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View !Access Denied! ", details: params[:id])
    flash[:notice] = "You don't have access to view Addresss."
    redirect_to roles_path
    end
end

def destroy
    if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:candelete?)
        @addresses = Address.find(params[:id])
        @logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Delete", details: params[:id])
        @addresses.destroy
        redirect_to roles_path
        flash[:notice] = 'Address Deleted'
    else
    flash[:notice] = "You don't have access to delete Addresss."
    redirect_to role_path
    end
end

# Before Filters

# Confirms if User is logged-in
def logged_in_user
 unless logged_in?
 flash[:danger] = "Please log in."
 redirect_to root_path
 end
end

2 个答案:

答案 0 :(得分:1)

您需要的是多态关联和嵌套属性。看看这个blog post,它们描述了这两件事情,然后带你完成迁移,修改模型和创建共享视图的步骤(他们的例子甚至是地址:))

答案 1 :(得分:0)

我找到了解决方案。已更改的帐户和联系人模型具有多个地址,并使用多种语言将地址模型与许多帐户和联系人相关联。

我跟着红宝石指南:

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations