Rails表单:为一个复选框输入

时间:2017-04-21 12:55:18

标签: ruby-on-rails forms

我有一个Rails表单,我希望允许用户使用复选框输入选择一个或多个GitHub存储库。几小时后浏览网页,我找不到解决方案。现在我有一个fields_for表单显示每个存储库的名称。

<%= form_for(quote) do |f| %>
  <% if quote.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(quote.errors.count, "error") %> prohibited this quote from being saved:</h2>

      <ul>
      <% quote.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %>
    <%= f.text_field  :title %>
  </div>

  <div class="field">
    <%= f.label :introduction %>
    <%= f.text_field  :introduction %>
  </div>

  <%= f.fields_for :repo do |r| %>
    <% while @i < @length_repo do %>
      <div class="field">
        <%= r.label @repo_data[@i]["name"] %>
        <%= check_box_tag 'repo[name][]', @repo_data[@i]["name"] %>
      </div>

      <div class="field">
        <%= r.hidden_field :description, value: @repo_data[@i]["description"] %>
      </div>

      <div class="field">
        <%= r.hidden_field :language, value: @repo_data[@i]["language"] %>
      </div>

      <div class="field">
        <%= r.hidden_field :stargazers_count, value: @repo_data[@i]["stargazers_count"] %>
      </div>

      <div class="field">
        <%= r.hidden_field :forks_count, value: @repo_data[@i]["forks_count"] %>
      </div>

      <% @i +=1 %>
    <% end %>
  <% end %>

  <div class="field">
    <%= f.hidden_field :user_id, value: @user %>
  </div>

  <%= f.fields_for :item do |i| %>
    <div class="field">
      <%= i.label 'Item Title' %>
      <%= i.text_field  :title %>
    </div>

    <div class="field">
      <%= i.label 'Item Description' %>
      <%= i.text_field  :content %>
    </div>

    <div class="field">
      <%= i.label 'Pricing Type' %>
      <%= i.select(:pricing_type, options_for_select([['Hourly Rate', 'hourly'], ['Daily Rate', 'daily'], ['Fixed Rate', 'fixed']], 2)) %>
    </div>

    <div class="field">
      <%= i.label :rate %>
      <%= i.text_field  :rate %>
    </div>

    <div class="field">
      <%= i.label :quantity %>
      <%= i.text_field  :quantity %>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :footer %>
    <%= f.text_field  :footer %>
  </div>

  <div class="field">
    <%= f.label :currency %>
    <%= f.select(:currency, options_for_select([['€', 'EUR'], ['$', 'DOL']], 2)) %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我添加了hidden_​​fields以将其他值保存到数据库中。如何保存与所选输入相对应的字段?我是否必须更改表单,或者最好在控制器中编写函数?

我的报价控制器def_params:

def quote_params
  params.require(:quote).permit(:title, :introduction, :footer, :currency, :user_id, :datetime, item_attributes: [ :title, :content, :pricing_type, :rate, :quantity ], repo_attributes: [ :name, :description, :language, :stargazers_count, :forks_count ])
end

Controller Quote,new and create:

# GET /quotes/new
  def new
    @quote = Quote.new
    @quote.item.build
    @quote.repo.build
    @user = current_user.id
end

# POST /quotes
  # POST /quotes.json
  def create
    @quote = Quote.new(quote_params)
    @user = current_user.id

    respond_to do |format|
      if @quote.save
        format.html { redirect_to @quote, notice: 'Quote was successfully created.' }
        format.json { render :show, status: :created, location: @quote }
      else
        format.html { render :new }
        format.json { render json: @quote.errors, status: :unprocessable_entity }
      end
    end
  end

我的回购模式:

class Repo < ApplicationRecord
  belongs_to :quote
end

我的报价模型:

class Quote < ApplicationRecord
  has_one :contact
  has_many :reference
  has_many :item, inverse_of: :quote
  has_one :analytic
  has_many :question
  has_many :repo
  belongs_to :user

  accepts_nested_attributes_for :item
end

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

试试这个

= check_box_tag 'quote[repo_attributes][name][]', @repo_data[@i]["name"]

希望有所帮助!