Rails check_box_tag参数在第

时间:2016-03-07 20:04:02

标签: ruby-on-rails nested-forms

新手RoR,他疯了!

当我勾选几个check_box并单击“提交”按钮时。参数框(值数组)在Param中不可用。然后页面重定向回自身。当我再次做同样的事情时(勾选几个check_box并单击Submit),然后复选框中的复选框可用,我可以保存它们。第一次出了什么问题,如何解决它。我在这做错了什么?

非常感谢!

<div class="row">
<div class="col-md-4">
 <%= form_for(@user, url: registration_path(@user), html: { method: :put }) do |f| %>
 <%= devise_error_messages! %>

 <div class="field">
  <%= f.label :email %><br>
  <p><%= f.email_field :email, autofocus: true %></p>
</div>

<!-- <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
  <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>

<div class="field">
  <%= f.label :password, "New password" %><br />
  <%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
  <%= f.label :password_confirmation, "New password confirmation"  %><br />
  <%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="field">
  <%= f.label :current_password %> <i>(to confirm changes)</i><br />
  <%= f.password_field :current_password, autocomplete: "off" %>
</div> -->


<%= form_for (@user.musician) do |musician_form| %>
  <%= musician_form.label "Naam" %>
  <p><%= musician_form.text_field :name %></p>

  <%= musician_form.label "Wat is je woonplaats?" %>
  <p><%= musician_form.text_field :city %></p>

  <%= musician_form.label "In welke Provincie woon je?" %><br>
  <%= musician_form.select :province_id, Province.all.collect{|t| [t.description, t.id]}, { :include_blank => false } %><br><br>

  <%= musician_form.label "Wat is je geboortedatum?" %>
  <p><%= musician_form.date_select :birthday %></p>

  <%= musician_form.label "Beschrijving" %><br>
  <%= musician_form.text_area :description, class: "form-control", size: "40x5" %>

<div class="actions">
  <br>
  <%= f.submit "Wijzigingen opslaan", class: "btn btn-primary btn-xs" %>
</div>

<div class="col-md-4">
<b>Welk type muzikant ben je?</b><br><br>

 <% for musician_type in MusicianType.all %>
  <div>
   <%= check_box_tag "musician[musician_type_ids][]", musician_type.id, @user.musician.musician_types.include?(musician_type) %>
  <%= musician_type.description %>
  </div>
<% end %>

<br>
<b>In welke genre(s) ben je geinteresseerd?</b><br><br>
<% for genre in Genre.all %>
  <div>
   <%= check_box_tag "musician[genre_ids][]", genre.id, @user.musician.genres.include?(genre) %>
   <%= genre.description %>
   </div>
  <% end %>
 <br>

   <% end %> 
<% end %>

<div class="col-md-4">
  <p>Niet tevreden? <%= button_to "Account verwijderen", registration_path(resource_name), data: { confirm: "Weet je zeker dat je je account en bijbehorende band(s) en vacature(s) wilt verwijderen? Het account kan niet meer worden teruggehaald!" }, method: :delete, class: "btn btn-danger btn-xs" %></p>
</div>

这是我的控制器代码的一部分:

def edit
 @user = User.find(current_user.id)
end

def update
  account_update_params =     devise_parameter_sanitizer.sanitize(:account_update)
  @user = User.find(current_user.id)

  params[:musician][:musician_type_ids] ||= []
  params[:musician][:genre_ids] ||= []

# unless params[:musician][:musician_type_ids].present?
#   params[:musician][:musician_type_ids] = []
#   logger.info "DEBUG INFO: PARAM IS EMPTY #{@musician}"
# end
#
# unless params[:musician][:genre_ids].present?
#   params[:musician][:genre_ids]  = []
# end

@musician = current_user.musician
@musician.update(musician_params)

if needs_password?
  successfully_updated = @user.update_with_password(account_update_params)
else
  account_update_params.delete('password')
  account_update_params.delete('password_confirmation')
  account_update_params.delete('current_password')
  successfully_updated = @user.update_attributes(account_update_params)
end

if successfully_updated
  set_flash_message :notice, :updated
  sign_in @user, :bypass => true
  redirect_to edit_user_registration_path
else
  render 'edit'
 end
end

1 个答案:

答案 0 :(得分:0)

查看apidock上的文档:

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag

在评论中:

<% @roles.each do |role| %>
  <li>
    <%= check_box_tag 'role_ids[]', role.id -%>
    <%= h role.name -%>
  </li>
<% end %>

因此,如果你想允许genre_ids,你必须写:

<%= check_box_tag "genre_ids[]", genre.id, @user.musician.genres.include?(genre) %>