大家好, 我一直在搜索stackoverflow,但还没有找到答案,所以我直接询问。
我有一个名为message的简单表,其中包含用户ID和内容。我现在有第二个和第三个表,其中包含消息表中的message_id,一个recipient_id和一个可见性。
当我提交表单时,我总是有相同的错误:未允许的参数:message_recipients,message_groups。即使我在许可证中添加它们......
让我告诉你:
Message.rb
class Message < ActiveRecord::Base
include Tenantable
belongs_to :user
has_many :message_recipients
has_many :message_groups
validates :content, :user, presence: true
default_scope { order(created_at: :desc) }
end
message_recipient.rb
class MessageRecipient < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
message_group.rb
class MessageGroup < ActiveRecord::Base
belongs_to :user_group
belongs_to :message
end
messages_controller.rb
class MessagesController < ApplicationController
# GET /messages/new
def new
@message = Message.new
end
# POST /messages
def create
@message = Message.new(message_params)
@message.user = current_user
respond_to do |format|
if @message.message_recipients.empty?
format.html { render :new}
else
if @message.save
@message.message_recipients.user = current_user
@message.message_recipients.message = @message
format.html { redirect_to authenticated_root_path, notice: t('messages.created') }
else
format.html { render :new }
end
end
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def message_params
params.require(:message).permit(:content, { message_recipient_ids: [], message_group_ids: [] })
end
end
_form.html.erb
<% if @message.errors.any? %>
<div class="alert alert-danger" role="alert">
<h4><span class="glyphicon glyphicon-alert" aria-hidden="true"></span> <%= t('activerecord.errors.record_invalid', count: @message.errors.count) %></h4>
<ul>
<% @message.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for(@message, html: { class: 'form-horizontal' }) do |f| %>
<div class="form-group">
<%= f.label :message_recipients, class: 'col-sm-2 control-label' %>
<div class="col-sm-3">
<%= f.select :message_recipients, options_for_select(User.all.map { |s|[s.firstname + ' ' + s.lastname, s.id] }) %>
</div>
</div>
<div class="form-group">
<%= f.label :message_groups, class: 'col-sm-2 control-label' %>
<div class="col-sm-3">
<%= f.select :message_groups, options_for_select(UserGroup.all.map { |s|[s.name, s.id] }) %>
</div>
</div>
<div class="form-group">
<%= f.label :content, class: 'col-sm-2 control-label' %>
<div class="col-sm-3">
<%= f.text_area :content, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-2">
<button type="submit" class="btn btn-primary"><%= t('actions.save') %></button>
</div>
</div>
<% end %>
答案 0 :(得分:0)
在表单中,更改字段名称以匹配控制器中允许的字段名称。
:message_recipients
至":message_recipient_ids"
答案 1 :(得分:0)
在这里,应该这样做。不完全是最好的解决方案,但仍然是有效的解决方案
class MessagesController < ApplicationController
# GET /messages/new
def new
@message = Message.new
end
# POST /messages
def create
@message = Message.new(message_params)
@message.user = current_user
if @message.message_recipients.empty?
format.html { render :new}
else
if @message.save
create_message_recipients
create_message_groups
format.html { redirect_to authenticated_root_path, notice: t('messages.created') }
else
format.html { render :new }
end
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def message_params
params.require(:message).permit(:content)
end
def create_message_recipients
params[:message][:message_recipients].each do |user_id|
MessageRecipient.create(user_id: user_id, message_id: @message.id)
end
end
def create_message_groups
params[:message][:message_groups].each do |group_id|
MessageGroup.create(group_id: group_id, message_id: @message.id)
end
end
end