[英]Rails 4 HABTM Select2
保存client_ids時遇到小問題。 我不明白我要去哪里錯了。 基本上我有3個型號
客戶,消息和代表。 我想同時向多個客戶端發送消息。 我正在嘗試使用select2。
class Client < ActiveRecord::Base
scope :finder, lambda { |q| where("last_name ILIKE :q", q: "%#{q}%") }
def as_json(options)
{ id: id, text: name }
end
has_many :delegateships
has_many :messages, through: :delegateships
CLIENT_SALUTATIONS = [
'Mr.',
'Mrs.',
'Miss.',
'Ms.',
'Dr.'
]
def name
(salutation? && first_name? && last_name?) ? [salutation.capitalize, first_name.capitalize, last_name.capitalize ].join(" ") : email
end
end
我的留言模型
class Message < ActiveRecord::Base
has_many :delegateships
has_many :clients, through: :delegateships
end
代表模式
class Delegateship < ActiveRecord::Base
belongs_to :message
belongs_to :client
end
messages.js.coffee
$ ->
$('.select2-autocomplete').each (i, e) ->
select = $(e)
options = {
multiple: true
}
options.ajax =
url: select.data('source')
dataType: 'json'
data: (term, page) ->
q: term
page: page
per: 5
results: (data, page) ->
results: data
options.dropdownCssClass = 'bigdrop'
select.select2 options
form.html.haml
= simple_form_for(@message) do |f|
= f.error_notification
.form-inputs
= hidden_field :client_ids, '', data: { source: clients_path }, class: 'select2-autocomplete form-control', name: "message[client_ids][]"
= f.association :event, input_html: { id: 'e1'}
= f.input :content
.form-actions
= f.button :submit
訊息控制器
class MessagesController < ApplicationController
private
def message_params
params.require(:message).permit(:event_id, :content, :user_id, client_ids: [:id]
)
end
end
ClientsController
class ClientsController < ApplicationController
def index
@clients = Client.all
respond_to do |format|
format.html
format.json { render json: @clients.where('last_name ILIKE ?', "%#{params[:q]}%"), only: [:id, :last_name] }
end
end
end
終奌站
Started PATCH "/messages/13i84tc" for 127.0.0.1 at 2014-07-04 09:41:48 +0200
Processing by MessagesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"M5K47IOTTcOF1natNL/6Md4HEKzAJHZABHOa6fOumWw=", "message"=>{"client_ids"=>["3,4,2"], "event_id"=>"1", "content"=>"Dear Sir, \r\n\r\nI thought i would reach out to you regarding the event. "}, "commit"=>"Update Message", "id"=>"13i84tc"}
Message Load (0.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."slug" = '13i84tc' ORDER BY "messages"."id" ASC LIMIT 1
(0.1ms) BEGIN
Client Load (0.3ms) SELECT "clients".* FROM "clients" INNER JOIN "delegateships" ON "clients"."id" = "delegateships"."client_id" WHERE "delegateships"."message_id" = $1 [["message_id", 11]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/messages/13i84
客戶端顯示沒有任何問題,但我無法讓他們保存在委托表上。 我查看了以下內容以嘗試解決我的問題:
http://gistflow.com/posts/428-autocomplete-with-rails-and-select2 http://luksurious.me/?p=46 http://railscasts.com/episodes/17-habtm-checkboxes-revised http://railscasts.com/episodes/258-token-fields-revised http://ivaynberg.github.io/select2/
我會嘗試從您的strong_params中刪除:id
,因為這可能是問題的根源,並且它會忽略實際的數組,因為client_ids
不是哈希數組。 您的語法在這里可能會導致它忽略正在發送的數據。
看一下我在這里做的這個例子: https : //github.com/excid3/habtm_example
克隆,捆綁,耙db:migrate和rails使其啟動並運行。 它具有/ clients和/ messages路由。 創建一些客戶端,然后測試向這些客戶端創建消息。
特別是/ messages / new表單。
<%= hidden_field_tag "message[client_ids][]", nil %>
<%= f.select :client_ids, Client.all.map{ |c| [c.name, c.id]}, {}, multiple: true %>
這將設置一個選擇框,以在發送消息時從所有可用客戶端中進行選擇。 它會將client_ids
數組傳遞給Rails知道的使用關系設置的消息。
您還需要告訴MessageController接受帶有strong_params的client_ids數組:
def message_params
params.require(:message).permit(:body, client_ids: [])
end
它還使用select2來設置選擇框的樣式。
select2 style wont apply. i just want a simple drondown
_form.html.erb
<div class="col-sm-8" name="account">
<%= hidden_field_tag "message[branch_id][]", nil%>
<%= f.select :branch_id, Branch.all.map{ |c| [c.branch_name, c.id]}, {} %>
</div>
</div>
$("#message_branch_id").select2({
placeholder: "Select a Branch",
allowClear: true
});
def course_params
params.require(:course).permit(:name, :start_date, :end_date, :branch_id)
end
class Branch < ActiveRecord::Base
has_many :courses
end
class Course < ActiveRecord::Base
belongs_to :branch
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.