2017-04-21 67 views
0

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

<%= 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 

控制器报价,新创建:

# 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

试试这个

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

希望帮助!

+0

谢谢。字段在窗体中很好地显示,但是存储库值不会保存到数据库中。 (我在我的问题中添加了我的引用控制器的新动作) – AlphaNico

+0

我在问题 – AlphaNico

+0

中添加了完整的表单请尝试我的编辑答案 – RSB