在Rails中进行多选多选

时间:2018-12-12 17:57:47

标签: ruby-on-rails ruby-on-rails-4

我有一个名为Article的模型,另一个名为Organization的模型和一个名为ArticleAllowedOrganization的联接表,其中有一个article_id和organization_id。

当用户创建文章时,我希望他们指定哪些组织应有权访问该文章。

当我在控制台中测试时,它似乎可以正常工作,但是,当我在Web应用程序中创建文章时,直通关联allowed_organization_ids的参数为空。

商品模型:

class Article < ActiveRecord::Base
  has_many :article_allowed_organizations, dependent: :destroy
  has_many :allowed_organizations, through: :article_allowed_organizations, source: :organization
end

组织模式:

class Organization < ActiveRecord::Base
    has_many :article_allowed_organizations, dependent: :destroy
    has_many :allowed_articles, through: :article_allowed_organizations, source: :article
end

联接表:

class ArticleAllowedOrganization < ActiveRecord::Base
  belongs_to :article
  belongs_to :organization
end

在articles_controller.rb中,我允许使用allowed_organization_ids数组

def article_params
  params.require(:article).permit(:other_stuff, :allowed_organization_ids => [])
end

形式:

  <%= f.collection_select(:allowed_organization_ids, Organization.order(:name), :id, :name,
                                    {include_blank: true}, {multiple: true}) %> 

在控制台中,我可以手动设置要与文章相关联的多个组织ID,并且它可以工作。

     a = Article.last
     a.allowed_organization_ids = [2, 4]

您可以在下面看到,当我使用控制台时,它将组织的ID插入ArticleAlllowedOrganization表中。

 (0.3ms)  BEGIN
  SQL (1.3ms)  INSERT INTO "article_allowed_organizations" ("article_id", "organization_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["article_id", 95], ["organization_id", 4], ["created_at", "2018-12-12 17:47:25.978487"], ["updated_at", "2018-12-12 17:47:25.978487"]]
   (6.5ms)  COMMIT
=> [2, 4]

使用网络应用程序时,我会在日志中看到allowed_organization_ids没有传递任何内容。

  Parameters: {"....some_other_params" "allowed_organization_ids"=>[""]}

如果我尝试使用collection_check_boxes

  <%= f.collection_check_boxes :allowed_organization_ids, Organization.all, :id, :name do |b| %>
     <div class="collection-check-box">
       <%= b.check_box %>
       <%= b.label %>
     </div>
 <% end %>

使用collection_check_boxes时,它在日志中显示为true,而不是组织ID,这在日志中得到了

"allowed_organization_ids"=>["true"]

还有一个有趣的事情是,当我尝试使用复选框来查找ID为0但不能阻止表单提交的组织时,我也在博客中看到了以下内容。

  [1m[35mOrganization Load (0.5ms)[0m  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT 1  [["id", 0]]
Completed 404 Not Found in 67ms (ActiveRecord: 2.4ms)

ActiveRecord::RecordNotFound (Couldn't find Organization with 'id'=0):

1 个答案:

答案 0 :(得分:1)

请检查以下问题。在您的控制器中,您放置了

def article_params
  params.require(:article).permit(:other_stuff, :allowed_organization_ids => [])
end

所以您已经说出allowed_organization_ids为空,请替换为以下内容

def article_params
  params.require(:article).permit(:other_stuff, :allowed_organization_id)
end

我认为您只是在更改名称,所以您提供了不同的类名称,但是在关系中您提到的是不同的,但是由于您的控制台提供了正确的关系,这应该不是问题,但万一我在提起它。

class ArticleAllowedOrganization < ActiveRecord::Base
  belongs_to :article
  belongs_to :organization
end

应该被allowed_article和allowed_organization

我正在练习,所以我在项目中创建了它,以查看它是否使用适当的名称,因此下面是我在生成器中用于创建整个项目的命令。您可以尝试查看是否还有其他错误。注意我将权限字段放在允许的表中,所有命令都在项目内部的命令行上,第一个命令将创建新项目。

  rails new SampleProject && cd $_ && bundle 

  rails g model Article title:string description:text
  rails g model Organization name:string
  rails g model Allowed permission:string article:references organization:references

现在确保您的Article模型具有以下关注对象。

class Article < ActiveRecord::Base
  has_many :alloweds
  has_many :organizations, through: :alloweds
end

在您的组织模型中

类组织

在您允许的模型中,尽管我将使用允许的表名,而不是允许的表名,但这是您的命名。

class Allowed < ActiveRecord::Base
 belongs_to :article
 belongs_to :organization
end

现在让我们生成视图和控制器

rails g scaffold_controller Article title:string description:string
rails g scaffold_controller Organization name:string
rails g scaffold_controller Article title:string description:string

现在将路由放入您的config / routes.rb

resources :articles
resources :organizations
resources :alloweds

现在,您可以启动铁路服务器,并导航到/ articles,/ organization和/ alloweds,以查看其工作情况。 Rails自动生成器将文本字段用作关系ID,因此您可以手动放置ID,它将起作用。

希望很清楚。