这是一个非常常见的问题,有很多博文都涵盖了这一点。我只是不知道自己做错了什么。 很抱歉这篇长篇文章。
我有Mono-Transitive many-to-many relationship。
会话可能有多个字幕集。字幕主义者可能有多个会话。多对多关系由" Membership"完成。
class Session < ApplicationRecord
# This will tell to delete all the memberships
# in case the current session is deleted.
has_many :memberships, dependent: :destroy
# Added this, see:
# https://www.sitepoint.com/master-many-to-many-associations-with-activerecord/
has_many :captionists, through: :memberships
# https://www.sitepoint.com/complex-rails-forms-with-nested-attributes/
# Allows us to delete the membersihps if not included.
accepts_nested_attributes_for :memberships, :allow_destroy => true
validates :uuid, presence: true, uniqueness: true
validates :name, presence: true, uniqueness: false
validates :url, presence: true, uniqueness: true
validates :passcode, presence: true, uniqueness: true
validates :shortcode, presence: true, uniqueness: true
end
class Captionist < ApplicationRecord
has_many :memberships
# Added this, see:
# https://www.sitepoint.com/master-many-to-many-associations-with-activerecord/
has_many :sessions, through: :memberships
validates :uuid, presence: true, uniqueness: true
validates :full_name, presence: true, uniqueness: false
validates :access_token, presence: true, uniqueness: true
validates :passcode, presence: true, uniqueness: false
end
# Mono Transitive Association
# https://www.sitepoint.com/master-many-to-many-associations-with-activerecord/
class Membership < ApplicationRecord
belongs_to :session
belongs_to :captionist
validates :uuid, presence: true, uniqueness: true
validates :hostname, presence: true, uniqueness: false
validates :web_port, presence: true, uniqueness: false
validates :tcp_port, presence: true, uniqueness: false
end
这是我的session_controller(它位于管理员命名空间下)
module Admin
class SessionsController < Admin::BaseAdminController
def index
# List of sessions
# Careful on not to iterate through everything, as Rails will interpret it
# as all.
# Default is #25
# This uses the kaminari gem.
# https://github.com/kaminari/kaminari
@sessions = Session.page(params[:page] || 1)
@page_count = @sessions.total_pages
end
def edit
@session = Session.includes(:memberships).includes(:captionists).find(params[:id])
if !@session
raise ActionController::RoutingError.new('Not Found')
end
end
def update
@session = Session.find(params[:id])
if @session.update_attributes(session_params)
respond_to do |format|
format.html { redirect_to edit_admin_session_path , notice: 'Session was successfully updated.' }
end
return
end
render 'edit'
end
def session_params
return params.require(:session).permit(:name, :passcode, :shortcode,
membership_attributes: [:id, :hostname, :tag_teaming, :web_port, :tcp_port, :allowing_connections, :_destroy])
end
end
end
最后,但并非最不重要的是.erb文件:
<h1>Editting!!</h1>
<%= link_to 'Go Back To list', admin_sessions_path %>
<%= form_for :session, method: :patch, class:'container' do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %><br />
</div>
<div class="form-group">
<%= f.label :passcode %>
<%= f.text_field :passcode, class: 'form-control' %><br />
</div>
<%= f.label :shortcode %>
<%= f.text_field :shortcode, class: 'form-control' %><br />
<%= f.label :memberships %>
<table class="table table-striped table-hover">
<thead class="thead-dark">
<tr>
<th scope="col"> Allowing Connections </th>
<th scope="col">Tag Teaming</th>
<th scope="col">Hostname</th>
<th scope="col">Web Port</th>
<th scope="col">TCP Port</th>
<th scope="col">Captionist Name </th>
<th scope="col">Action</th>
</tr>
</thead>
<!-- HERE'S THE PART THAT is giving me the problem -->
<% @session.memberships.map do |membership| %>
<%= f.fields_for membership do |ff| %>
<tr class="nested-fields">
<%= ff.hidden_field :id %>
<td>
<%= ff.label :allowing_connections %>
<%= ff.check_box :allowing_connections %>
</td>
<td>
<%= ff.label :tag_teaming %>
<%= ff.check_box :tag_teaming %>
</td>
<td>
<%= ff.label :hostname %>
<%= ff.text_field :hostname %>
</td>
<td>
<%= ff.label :web_port %>
<%= ff.text_field :web_port %>
</td>
<td>
<%= ff.label :tcp_port %>
<%= ff.text_field :tcp_port %>
</td>
<td>
<%= membership.captionist.full_name %>
</td>
<td>
<%= ff.check_box :_destroy %>
</td>
</tr>
<% end %>
<% end %>
</table>
<%= f.submit 'Update', class: 'btn btn-primary' %>
<% end %>
以下是发送的参数:
{"name"=>"Session Correct name", "passcode"=>"A Passcode", "shortcode"=>"A Shortcode", "membership"=>{"id"=>"106", "allowing_connections"=>"0", "tag_teaming"=>"0", "hostname"=>"delete hostname", "web_port"=>"80", "tcp_port"=>"3001", "_destroy"=>"0"}} permitted: false>
以下是我将params列入白名单的方法:
return params.require(:session).permit(:name, :passcode, :shortcode,
membership_attributes: [:id, :hostname, :tag_teaming, :web_port, :tcp_port, :allowing_connections, :_destroy])
有什么问题?
1)现在,如果我提交表格,我会得到:unpermitted_params = [&#34;会员资格&#34;]在控制台的末尾 2)我已检查数据库是否已正确迁移。外键已正确设置。我可以毫无问题地浏览属性。我所做的是在执行迁移后添加has_many关系。我不知道是否需要添加某种迁移才能使更新生效。 3)我试过替换
<%= form_for :session, method: :patch, class:'container' do |f| %>
为: &lt;%= form_for @session,方法:: patch,class:&#39; container&#39;做| f | %GT;
但我收到错误:
无法找到#
的有效映射
4)我还尝试删除<% @session.memberships.map do |membership| %>
,但我不会填充字段。我看到的另一个问题是输入的名称都是相同的。每当我发送参数进行更新时,它只会反映出最后一个嵌套部分。
这是来自服务器的响应(params [:session]):
Parameters {"name"=>"Session Correct name", "passcode"=>"A Passcode", "shortcode"=>"A Shortcode", "membership"=>{"id"=>"106", "allowing_connections"=>"0", "tag_teaming"=>"0", "hostname"=>"delete hostname", "web_port"=>"80", "tcp_port"=>"3001", "_destroy"=>"0"}} permitted: false>
不嵌套得到更新的部分。
有什么想法吗?
谢谢!
编辑(2018年4月9日):添加了发送的参数(我在这里没有)。修复了格式和HTML的问题。
答案 0 :(得分:1)
我很确定这只是一个多元化的问题。我在这里与你的代码相似,看起来你想要memberships_attributes
在强大的参数中。
同时检查正在生成的HTML,并确保您允许的密钥匹配。
我建议在控制器内放置一个binding.pry或者你喜欢的任何调试,并尝试使用正在进入的参数保存对象。
答案 1 :(得分:1)
您不需要:
.slider-progress-bar {
/* your progress bar styles here */
.progress {
height: 4px;
background: red;
animation: sliderProgressBar ease;
}
}
.my-slider:hover {
.slider-progress-bar .progress {
animation-play-state: paused;
}
}
@keyframes sliderProgressBar {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
你只需要:
<% @session.memberships.map do |membership| %>
<%= f.fields_for membership do |ff| %>
Rails将遍历会话中的所有成员资格,并将所有字段放在memberships_attributes中。