1

I have a Rails form and I want to allow the user to select one or more of his GitHub repositories with checkbox inputs. After browsing the web during hours, I can't find a solution for this. Right now I have a fields_for form displaying the names of each repository.

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

I added hidden_fields to save the other values to the database. How can I save the fields corresponding to the input checked? Do I have to change my form or it's better to write a function in the controller?

My Quote controller 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

My repo model:

class Repo < ApplicationRecord
  belongs_to :quote
end

My quote model:

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

Thanks for your help.

1 Answer 1

2

Try this instead

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

Hope that helps!

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. Fields display well in form but repository values are not saved to the database. (I added the new action of my quote controller in my question)
I added the full form in the question
It seems to be working, I just get the following error from my controller: unknown attribute 'repo_attributes' for Quote. I added the create controller to the question. Any idea why I get this? Should I pass the "repo_attributes" as argument in @quote = Quote.new(quote_params)?
Edit: I can save the form, but it does not save to the Repo Table. I will investigate further!
Can you paste the params hash you are receiving in controller?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.