簡體   English   中英

Rails 5 has_and_belongs_to_many形式

[英]Rails 5 has_and_belongs_to_many form

我有一個具有客戶端和站點模型的小應用程序。 我想從顯示頁面以模態創建一個新站點,但我卻出錯了。

請在下面找到模式,控制器,模型和錯誤。

模式

create_table "clients", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "clients_sites", id: false, force: :cascade do |t|
    t.bigint "client_id", null: false
    t.bigint "site_id", null: false
  end

create_table "sites", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

我想從我的客戶#顯示頁面創建一個網站

<div class="page-header">
  <%= link_to clients_path, class: 'btn btn-default' do %>
    <span class="glyphicon glyphicon-list-alt"></span>
    All Clients
  <% end %>
  <%= link_to edit_client_path(@client), class: 'btn btn-primary' do %>
    <span class="glyphicon glyphicon-pencil"></span>
    Edit
  <% end %>
  <h1>Show client</h1>
</div>

<dl class="dl-horizontal">
  <dt>Name:</dt>
  <dd><%= @client.name %></dd>

</dl>


<div class="row">
  <div class="col-sm-6">
    <h1>Sites</h1>
  </div>

  <div class="col-sm-6 text-right">
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Add New Site
    </button>

    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <%= form_for [@client, @site] do |f| %>
              <%= form.label :name %>
              <%= form.text_field :name, class: 'form-control' %>
              <%= form.submit class: 'btn btn-primary' %>
            <% end %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="table-responsive">
  <table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>

      <th>Name</th>



    </tr>
    </thead>

    <tbody>
    <% @client.sites.each do |site| %>
      <%= content_tag :tr, id: dom_id(site), class: dom_class(site) do %>

        <td><%= link_to site.name, site %></td>




      <% end %>
    <% end %>
    </tbody>
  </table>
</div>

客戶控制器

  def show
    @client = Client.find(params[:id])
    @site = Site.new
  end

應用程序/模型/ client.rb

class Client < ApplicationRecord
  has_and_belongs_to_many :sites
end

應用程序/模型/ site.rb

class Site < ApplicationRecord
  has_and_belongs_to_many :clients
end

當我點擊頁面時,出現以下錯誤

ActionView::Template::Error (undefined method `client_sites_path' for #<#<Class:0x00007f9329282490>:0x00007f9329279890>
Did you mean?  clients_path
               edit_site_path):
    39:             </button>
    40:           </div>
    41:           <div class="modal-body">
    42:             <%= form_for [@client, @site] do |f| %>
    43:               <%= form.label :name %>
    44:               <%= form.text_field :name, class: 'form-control' %>
    45:               <%= form.submit class: 'btn btn-primary' %>

app/views/clients/show.html.erb:42:in `_app_views_clients_show_html_erb___1421137287308647677_70135013712680'

編輯

路線

 sites GET    /sites(.:format)                                                                         sites#index
                           POST   /sites(.:format)                                                                         sites#create
                  new_site GET    /sites/new(.:format)                                                                     sites#new
                 edit_site GET    /sites/:id/edit(.:format)                                                                sites#edit
                      site GET    /sites/:id(.:format)                                                                     sites#show
                           PATCH  /sites/:id(.:format)                                                                     sites#update
                           PUT    /sites/:id(.:format)                                                                     sites#update
                           DELETE /sites/:id(.:format)                                                                     sites#destroy
                   clients GET    /clients(.:format)                                                                       clients#index
                           POST   /clients(.:format)                                                                       clients#create
                new_client GET    /clients/new(.:format)                                                                   clients#new
               edit_client GET    /clients/:id/edit(.:format)                                                              clients#edit
                    client GET    /clients/:id(.:format)                                                                   clients#show
                           PATCH  /clients/:id(.:format)                                                                   clients#update
                           PUT    /clients/:id(.:format)                                                                   clients#update
                           DELETE /clients/:id(.:format)                                                                   clients#destroy

Rails.application.routes.draw do
  resources :sites
  resources :clients

ActionView :: Template :: Error(針對類:0x00007f9329282490>:0x00007f9329279890的未定義方法“ client_sites_path”是什么意思?clients_path edit_site_path):

該錯誤表明沒有可用的名為client_sites_path路徑幫助器。 確實如此,因為您沒有以這種方式定義路線。 根據您對@arieljuod帖子的評論,我了解到您想將sites保存到clients 下面的代碼將幫助您實現所需的目標

<%= form_for @site do |f| %>
  <%= form.label :name %>
  <%= form.text_field :name, class: 'form-control' %>
  <%= form.select :client_ids, options_from_collection_for_select(Client.all, :id, :name), :prompt => "Select Clients", :multiple => true %>
  <%= form.submit class: 'btn btn-primary' %>
<% end %>

這段代碼片段

<%= form.select :client_ids, options_from_collection_for_select(Client.all, :id, :name), :prompt => "Select Clients", :multiple => true %>

創建一個下拉菜單 ,您可以在其中選擇一個或多個客戶端以添加到網站,這些客戶端將連同提交到sites#create操作的params一起提交。

另外,您還應確保將client_ids: [] 列入白名單 ,方法是將其添加到site_params方法中。 通過這樣做,Rails可以使用此client_ids值生成clients_sites表的條目。 從而完成了向clientssites創建。

當您將模型數組作為form_for幫助器的參數傳遞時,rails會嘗試查找該關系的路由(client_sites_path)。

您需要定義該路線。 使用url_for helpers https://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects查看Rails路由指南,嵌套資源部分和路徑。

將站點資源嵌套在客戶端資源內:

resources :clients do
  resources :sites
end

並檢查您的新路線,現在您應該在rake routes的輸出上看到一個client_sites路線,並且[@client, @site]應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM