这是我目前得到的:
Started POST "/duels" for 127.0.0.1 at 2016-10-15 02:51:06 -0400
Processing by DuelsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/vNueb1U+W5asQ4/Vnh5kSdeUc6uiaUKvWG1TIaAKWY0fIM2ovLlsLJUSsmgDG3nTbFgt7n8zSYYT2LkZEqLrA==", "duel"=>{"duelers_attributes"=>{"0"=>{"user_id"=>"2"}, "1"=>{"user_id"=>"15"}}, "consequence"=>"t", "reward"=>"t"}, "dueler"=>{"challenge_id"=>"141"}, "button"=>""}
只有一个challenge_id
正在通过,尽管应该有两个(每个人都有一个)。如何在提交时输出以下代码以输出这样的内容:
Started POST "/duels" for 127.0.0.1 at 2016-10-15 03:33:02 -0400
Processing by DuelsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xhbQpHgBmzfuvmYsyZx1Hss8CZBfQQN9+QCEciiq4yUMmT3rZ6eH6QZbIto/6GFoodM46Ug0a1FcLlPaymBB7w==", "duel"=>{"duelers_attributes"=>{"0"=>{"user_id"=>"2", "challenge_id"=>"141"}, "1"=>{"user_id"=>"15", "challenge_id"=>"143"}}, "consequence"=>"t", "reward"=>"t"}, "button"=>""}
duels_controller
def new
@duel = Duel.new
@user = User.find(params[:challenge_daddy])
@duel.duelers << Dueler.new(user_id: current_user.id, user_name: current_user.name, user_last_name: current_user.last_name)
@duel.duelers << Dueler.new(user_id: @user.id, user_name: @user.name, user_last_name: @user.last_name)
@current_user_challenges = current_user.challenges.order(:created_at)
@challenged_user_challenges = @user.challenges.order(:created_at)
respond_with(@duel)
end
决斗/新
<%= simple_form_for(@duel) do |f| %>
<%= f.fields_for(:duelers) do |dueler| %>
<%= render 'dueler_fields', :f => dueler %>
<% end %>
The loser will <%= f.text_field :consequence %>
<%= f.submit %>
<% end %>
_dueler_fields.html.erb
<%= f.select :user_id, User.order(:name).map { |user| [user.full_name, user.id] }, include_blank: true, id: "change-challenge-options" %>
<div id="current-user-challenges">
<%= f.collection_select :challenge_id, @current_user_challenges, :id, :full_challenge, include_blank: true %>
</div>
<div id="challenged-user-challenges">
<%= f.collection_select :challenge_id, @challenged_user_challenges, :id, :full_challenge, include_blank: true %>
</div>
<script> # If current_user then I show first collection_select otherwise I show second.
var userSelect = $('#change-challenge-options');
var currentUserId = $('#current-user-id').data('user-id');
$('#challenged-user-challenges').hide();
$('#current-user-challenges').hide();
userSelect.on('change', function() {
if (userSelect.val() == "0"){
$('#current-user-challenges').hide();
$('#challenged-user-challenges').hide();
}
else if (userSelect.val() == currentUserId) {
$('#current-user-challenges').show();
$('#challenged-user-challenges').hide();
} else {
$('#challenged-user-challenges').show();
$('#current-user-challenges').hide();
}
});
</script>
答案 0 :(得分:1)
如果您看到正在发送的params,您确实通过了两个挑战ID。 一个来自挑战的用户,另一个来自challanger。
您只需要在控制器中更好地访问它们并将其保存在适当的位置。
{
"utf8" =>"✓",
"authenticity_token" =>"...",
"duel" => {
"duelers_attributes" => {
"0" => {
"user_id" =>"2",
"challenge_id" =>"141"
},
"1" => {
"user_id" =>"15",
"challenge_id" =>"143"
}
},
"consequence" =>"t",
"reward" =>"t"
},
"button" =>""
}
我已将这些行分开以便更容易实现,但基本上在您的创建或更新方法中,您可以访问duel_params["duel]["0"]["challange_id"]
并将其保存到您想要的任何位置,duel_params["duel]["1"]["challange_id"]
您还可以更新活动记录回调中的一个或另一个,例如after_save或after_validation方法,无论哪种方式最适合您