how to insert multi textbox array in ruby on rails - ruby-on-rails

I'm new to ruby. i need insert the array textbox values to has_many and belongs_to relationship.i used two models intrrattes and intrsetups.
here is my new.html.erb file
<%= form_for #intrsetup do |f| %>
<div class='row'>
<div class='span6'>
<div class="control-group">
<label class=" control-label">Effective From<abbr title="required">*</abbr></label>
<div class="controls">
<%= f.text_field :effective_from, :onclick => "return calender()" %>
</div>
</div>
</div>
<div class='span6'>
<div class="control-group">
<label class=" control-label">Effective To</label>
<div class="controls">
<%= f.text_field :effective_to %>
</div>
</div>
</div>
</div>
<%= f.fields_for :intrrates do |builder| %>
<h3>Interest Rates</h3>
<table class='table condensed-table'>
<tr>
<td>
Days From
</td>
<td>
Days To
</td>
<td>
Rate
</td>
<td>
Senior Increment
</td>
<td>
Super Senior Increment
</td>
<td>
Widow Increment
</td>
</tr>
<tr>
<td>
<%(1..2).each do |i|%>
<%= builder.text_field(:days_from, :name => "intrrate[days_from][]", :id => "intrrate_days_from_#{i}") %>
<%end%>
<%= builder.text_field :days_to, multiple: true %>
<%= builder.text_field :rate, multiple: true %>
<%= builder.text_field :senior_increment %>
<%= builder.text_field :super_senior_increment %>
<%= builder.text_field :widow_increment %>
<% end %>
<%= f.submit %>
here is my Intrrate and Intrsetup model code
class Intrrate < ActiveRecord::Base
belongs_to :intrsetup
#attr_accessor :effective_from, :effective_to
attr_accessible :effective_from, :effective_to
attr_accessible :days_from, :days_to, :rate, :senior_increment, :super_senior_increment, :widow_increment, :intrsetup_id
end
class Intrsetup < ActiveRecord::Base
has_many :intrrates
accepts_nested_attributes_for :intrrates
attr_accessible :intrrates_id, :effective_from, :effective_to, :intrrates_attributes
end
here is my controller page
class IntrsetupsController < ApplicationController
def new
#intrsetup = Intrsetup.new
#intrrate = #intrsetup.intrrates.build
end
def create
#intrsetup = Intrsetup.new(params["intrsetup"])
#intrsetup.save
end
end
class IntrratesController < ApplicationController
def index
#intrrate = Intrrate.all
end
def new
#intrrate = Intrrate.new
end
def create
puts #intrrate = Intrrate.new(params["intrrate"])
#intrrate.save
end
end
my schema.rb
create_table "intrrates", :force => true do |t|
t.integer "days_from"
t.integer "days_to"
t.float "rate"
t.float "senior_increment"
t.float "super_senior_increment"
t.float "widow_increment"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "intrsetup_id"
t.integer "deposit_id"
end
create_table "intrsetups", :force => true do |t|
t.date "effective_from"
t.date "effective_to"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
my error page
NoMethodError in IntrsetupsController#create
undefined method `[]' for nil:NilClass
Rails.root: /home/tbf/rails_projects/ccddeposit
Application Trace | Framework Trace | Full Trace
app/controllers/intrsetups_controller.rb:9:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"WsfTU31o9LLfcoieNL3pgpRRu/swqreaXDdo6LxrdsM=",
"intrsetup"=>{"effective_from"=>"1994/12/06",
"effective_to"=>"1994/12/06"},
"intrrate_days_from_1"=>"1",
"intrrate_days_to_1"=>"45",
"intrrate_rate_1"=>"0.5",
"intrrate_senior_increment_1"=>"0.5",
"intrrate_super_senior_increment_1"=>"0.56",
"intrrate_widow_increment_1"=>"0.5",
"intrrate_days_from_2"=>"45",
"intrrate_days_to_2"=>"95",
"intrrate_rate_2"=>"0.5",
"intrrate_senior_increment_2"=>"0.7",
"intrrate_super_senior_increment_2"=>"0.8",
"intrrate_widow_increment_2"=>"0.5",
"commit"=>"Create Intrsetup"}
but i'm getting the following error
how to solve this error?

As I said, the problem is rate is attending a float and you give to it an Array.
So here is a code which force your parameter "rate" as a float value and give you the average of all rates entered in your form :
def create
# In case where you want the average value of all different rates you enter in your form
rate_avg = params["intrsetup"]["intrrates_attributes"]["0"]["rate"].inject(0.0) do |value, rate|
value += rate.to_f
end
params["intrsetup"]["intrrates_attributes"]["0"]["rate"] = rate_avg / params["intrsetup"]["intrrates_attributes"]["0"]["rate"].count
#intrsetup = Intrsetup.new(params["intrsetup"])
#intrsetup.save
end
Try this and tell me if it works now.

Related

Updating multiple models with one form in Rails

I am new to Rails and trying to build a form that allows users to complete and submit a form. The form completes their profile. I want the form to update the Users model (and database table) as well as the Interests model (hobbies). I am using a form with fields_for.
However submitting the form does not update the Interests model. It either only updates the User model or returns various errors (the following produces Interest user must exist error):
MODELS:
class User < ApplicationRecord
has_many :interests, foreign_key: "interests_user_id"
accepts_nested_attributes_for :interests
end
class Interest < ApplicationRecord
belongs_to :user
self.primary_key = "interest_id"
end
CONTROLLER:
class UsersController < ApplicationController
def index
#allUsers = User.all
end
def new
#user = User.new
#user.interests.build
end
def create
#user = User.new permitted_params
=begin
#user = User.new(
:author => params[:user][:author],
:user_name => params[:user][:user_name],
:user_bio => params[:user][:user_bio],
)
#user.interests.new(
:interest_name => params[:user][:interests_attributes][:interest_name],
:interest_experience_level => params[:user][:interests_attributes][:interest_experience_level],
:interest_positon => params[:user][:interests_attributes][:interest_positon],
)
=end
if #user.save
redirect_to users_url
else
render 'new'
end
end
private
def permitted_params
#params.require(:interest).permit(User.attribute_names.map(&:to_sym),interest.attribute_names.map(&:to_sym))
params.require(:user).permit(User.attribute_names.map(&:to_sym), interests_attributes: [:interest_name,:interest_experience_level,:interest_positon])
end
end
FORM:
<%= form_with model: #user do |newUser| %>
<% if #user.errors.any? %>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li> <%= message %></li>
<%end%>
</ul>
<%end%>
<div class="form-sections" id="user-profile">
<h3>General Information</h3>
<table class="formTables" id="formMainInfo">
<%= newUser.hidden_field :author, value:1 %>
<tr class="formRows">
<td class="formDescriptionCol"><%= newUser.label :user_name, "Your Name" %></td>
<td class="formInputCol">
<%= newUser.text_field :user_name %>
</td>
</tr>
<tr class="formRows">
<td class="formDescriptionCol"> <%= newUser.label :user_bio, "Biography" %></td>
<td class="formInputCol">
<%= newUser.text_area :user_bio %>
</td>
</tr>
<h3>Interests</h3>
<%= newUser.fields_for :interests do |newInterest| %>
<tr class="interestRows">
<td >Interest Name:</td>
<td>
<%= newInterest.text_field :interest_name%>
</td>
</tr>
<tr class="interestRows">
<td ><%=newInterest.label :interest_experience_level, "Experience Level" %></td>
<td >
<%= newInterest.text_field :interest_experience_level %>
</td>
</tr>
<tr class="interestRows">
<td ><%= newInterest.label :interest_position, "Position" %></td>
<td >
<%= newInterest.text_field :interest_position %>
</td>
</tr>
<%end%>
</table>
</div>
<div class="formSections" id="buttonsSection">
<%= newUser.submit "submit" %>
</div>
<%end%>
SCHEMA:
create_table "interests", primary_key: "interest_id", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "interest_user_id", null: false
t.string "interest_name", limit: 45, null: false
t.string "interest_experience_level", limit: 45, null: false
t.string "interest_position", limit: 45, null: false
t.index ["interest_user_id"], name: "user_id_idx"
end
create_table "users", primary_key: "user_id", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "user_name", limit: 60, null: false
t.string "author", limit: 60, null: false
t.string "user_bio", limit: 500, null: false
end
add_foreign_key "interests", "users", column: "interest_user_id", primary_key: "user_id", name: "interest_user_name", on_update: :cascade, on_delete: :cascade

Rails accepts_nested_attributes_for not working with has_many relationship and cocoon gem

I'm using the cocoon gem to build a form that creates a Tournament. A Tournament has_many Games. Cocoon lets me dynamically add more games to the form.
When I call #tournament.save, it generates the following errors:
Games team one must exist
Games team two must exist
tournament.rb
class Tournament < ApplicationRecord
has_many :games
accepts_nested_attributes_for :games, allow_destroy: true
end
game.rb
class Game < ApplicationRecord
belongs_to :tournament, optional: false
belongs_to :team_one, polymorphic: true
belongs_to :team_two, polymorphic: true
belongs_to :field, optional: true
end
schema.rb
ActiveRecord::Schema.define(version: 2019_12_24_011346) do
...
create_table "club_teams", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "fields", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "games", force: :cascade do |t|
t.bigint "tournament_id", null: false
t.string "team_one_type", null: false
t.bigint "team_one_id", null: false
t.string "team_two_type", null: false
t.bigint "team_two_id", null: false
t.bigint "field_id", null: false
t.date "date"
t.datetime "start_time"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["field_id"], name: "index_games_on_field_id"
t.index ["team_one_type", "team_one_id"], name: "index_games_on_team_one_type_and_team_one_id"
t.index ["team_two_type", "team_two_id"], name: "index_games_on_team_two_type_and_team_two_id"
t.index ["tournament_id"], name: "index_games_on_tournament_id"
end
create_table "high_school_teams", force: :cascade do |t|
t.string "school_name"
t.string "team_name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "tournaments", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "games", "fields"
add_foreign_key "games", "tournaments"
end
tournaments_controller.rb
class TournamentsController < ApplicationController
...
def create
#tournament = Tournament.new(tournament_params)
if #tournament.save
redirect_to #tournament
else
render 'new'
end
end
private
def tournament_params
params
.require(:tournament)
.permit(:name, games_attributes: [:id, :_destroy, :team_one_id, :team_two_id, :field_id, :date, :start_time])
end
end
request parameters in tournaments_controller#create
{
"authenticity_token"=>"iB4JefT9jRdiOFKok38OtjzMwd6Dv3hlHP/QZRtlFgMuVZfbn9PFD7Lebc1DuvfL6/IatDpS5CiubTci5MsCFg==",
"tournament"=>{
"name"=>"foo",
"games_attributes"=>{
"1577935885397"=>{
"team_one_id"=>"high-school-team-2",
"team_two_id"=>"club-team-2",
"date"=>"",
"start_time"=>"",
"_destroy"=>"false"
}
}
},
"commit"=>"Create Tournament"
}
tournament_params in tournaments_controller#create
<ActionController::Parameters {
"name"=>"foo",
"games_attributes"=><ActionController::Parameters {
"1577937916236"=><ActionController::Parameters {
"_destroy"=>"false",
"team_one_id"=>"high-school-team-2",
"team_two_id"=>"club-team-2",
"date"=>"",
"start_time"=>""
} permitted: true>
} permitted: true>
} permitted: true>
It seems to me that the tournament_params match what the accepts_nested_attributes documentation is expecting under One-to-many, so I don't see why there is an error.
Nested attributes for an associated collection can also be passed in
the form of a hash of hashes instead of an array of hashes:
Member.create(
name: 'joe',
posts_attributes: {
first: { title: 'Foo' },
second: { title: 'Bar' }
}
)
has the same effect as
Member.create(
name: 'joe',
posts_attributes: [
{ title: 'Foo' },
{ title: 'Bar' }
]
)
Edit:
tournaments/new.html.erb
<h1>Create a tournament</h1>
<%= render 'form' %>
<%= link_to 'Back', tournaments_path %>
tournaments/_form.html.erb
<%= form_with model: #tournament, class: 'tournament-form' do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<section class="games">
<%= f.fields_for :games do |game| %>
<%= render 'game_fields', f: game %>
<% end %>
<hr>
<p>
<%= link_to_add_association "Add game", f, :games,
data: {
association_insertion_node: '.games',
association_insertion_method: :prepend
}
%>
</p>
</section>
<p>
<%= f.submit %>
</p>
<% end %>
tournaments/_game_fields.html.erb
<section class="nested-fields">
<hr>
<p><strong>Game</strong></p>
<%= render "games/form_fields", f: f %>
<p><%= link_to_remove_association "Remove game", f %></p>
</section>
games/_form_fields.html.erb
<section>
<% if HighSchoolTeam.all.count + ClubTeam.all.count < 2 %>
<p>You neeed at least two teams to create a game. Create more high school and/or club teams first.</p>
<% else %>
<section class="game-form">
<p>
<%= f.label :team_one %><br>
<%= f.select :team_one_id, nil, {}, class: "team-one-dropdown" do %>
<optgroup label="High School Teams">
<% HighSchoolTeam.all.each do |high_school_team| %>
<option value="high-school-team-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
<% end %>
</optgroup>
<optgroup label="Club Teams">
<% ClubTeam.all.each do |club_team| %>
<option value="club-team-<%= club_team.id %>"><%= club_team.name %></option>
<% end %>
</optgroup>
<% end %>
</p>
<p>
<%= f.label :team_two %><br>
<%= f.select :team_two_id, nil, {}, class: "team-two-dropdown" do %>
<optgroup label="High School Teams">
<% HighSchoolTeam.all.each do |high_school_team| %>
<option value="high-school-team-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
<% end %>
</optgroup>
<optgroup label="Club Teams">
<% ClubTeam.all.each do |club_team| %>
<option value="club-team-<%= club_team.id %>"><%= club_team.name %></option>
<% end %>
</optgroup>
<% end %>
</p>
<p>
<%= f.label :field %><br>
<%= f.collection_select(:field_id, Field.all, :id, :name) %>
</p>
<p>
<%= f.label :date %><br>
<%= f.date_field :date %>
</p>
<p>
<%= f.label :start_time %><br>
<%= f.time_field :start_time %>
</p>
</section>
<% end %>
</section>
You seem to have problem saving team and not an issue of Cocoon gem.
Since you customize your select value to club-team-id and high-school-team-id. I think you just need to change it to something like this:
<option value="HighSchoolTeam-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
and
<option value="ClubTeam-<%= club_team.id %>"><%= club_team.name %></option>
Then the params will be
{
"authenticity_token"=>"iB4JefT9jRdiOFKok38OtjzMwd6Dv3hlHP/QZRtlFgMuVZfbn9PFD7Lebc1DuvfL6/IatDpS5CiubTci5MsCFg==",
"tournament"=>{
"name"=>"foo",
"games_attributes"=>{
"1577935885397"=>{
"team_one_id"=>"HighSchoolTeam-2",
"team_two_id"=>"ClubTeam-2",
"date"=>"",
"start_time"=>"",
"_destroy"=>"false"
}
}
},
"commit"=>"Create Tournament"
}
then you need to modified your params by:
# Adding before_action on top of your controller
before_action :modify_params, only: [:create, :update]
private
# Not the cleanest way, but this is what I can think of right now.
def modify_params
params.dig(:tournament, :games_attributes).each do |game_id, game_attribute|
team_one_type = game_attribute[:team_one_id].split('-').first
team_one_id = game_attribute[:team_one_id].split('-').last
team_two_type = game_attribute[:team_two_id].split('-').first
team_two_id = game_attribute[:team_two_id].split('-').last
params[:tournament][:games_attributes][game_id] = game_attribute.merge(
team_one_type: team_one_type,
team_one_id: team_one_id,
team_two_type: team_two_type,
team_two_id: team_two_id
)
end
end
# And update this method to allow team_one_type and team_two_type
def tournament_params
params.require(:tournament)
.permit(:name, games_attributes: [:id, :_destroy, :team_one_id, :team_two_id, :team_one_type, :team_two_type, :field_id, :date, :start_time])
end

Rails Multi-step form

I'm writing a quiz app with rails 5. I have got a multi-step form for question building.
Models:
class Mcq < ApplicationRecord
attr_accessor :option_count
has_many :options, dependent: :destroy
belongs_to :quiz
accepts_nested_attributes_for :options
validates :question_text, presence: true
end
class Option < ApplicationRecord
belongs_to :mcq, optional: true
validates :option_text, presence: true
end
Schema:
create_table "mcqs", force: :cascade do |t|
t.string "question_text"
t.boolean "required"
t.boolean "multiselect"
t.integer "quiz_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "options", force: :cascade do |t|
t.string "option_text"
t.integer "mcq_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
The first page is for question setup and has the following fields:
Option Count
Required (Yes / No)
No of options that can be selected (Single / Multiple)
The second page is for options and has the following fields:
Question Text
Nested Form for Options
Controller:
class McqsController < ApplicationController
def new
session[:current_step] ||= 'setup'
session[:mcq_params] ||= {}
#current_step = session[:current_step]
#quiz = Quiz.find(params[:quiz_id])
#mcq = Mcq.new(session[:mcq_params])
if session[:current_step] == 'options'
#option_count = session[:mcq_params]['option_count']
#option_count.times { #mcq.options.build }
end
end
def create
if params[:previous_button]
session[:current_step] = 'setup'
redirect_to new_quiz_mcq_path
elsif session[:current_step] == 'setup'
save_session(params[:mcq])
redirect_to new_quiz_mcq_path
elsif session[:current_step] == 'options'
#mcq = Mcq.new(whitelisted_mcq_params)
#mcq.quiz_id = params[:quiz_id]
#quiz = Quiz.find(params[:quiz_id])
if #mcq.save
session[:current_step] = session[:mcq_params] = nil
redirect_to quiz_new_question_path(#mcq.quiz_id)
else
#current_step = session[:current_step]
render :new
end
end
end
private
def whitelisted_mcq_params
params.require(:mcq)
.permit(:question_text, :multiselect, :required, options_attributes: [:option_text])
end
def save_session(mcq_params)
session[:mcq_params][:option_count] = mcq_params[:option_count].to_i
session[:mcq_params][:required] = mcq_params[:required]
session[:mcq_params][:multiselect] = mcq_params[:multiselect]
session[:current_step] = 'options'
end
end
The above solution works, but the code is messy and difficult to understand. I came across this railscasts episode which does something similar in a cleaner way. I've updated my code as follows:
class Mcq < ApplicationRecord
has_many :options, dependent: :destroy
belongs_to :quiz
attr_writer :current_step
attr_accessor :option_count
accepts_nested_attributes_for :options
validates :question_text, presence: true
def current_step
#current_step || steps.first
end
def steps
%w[setup options]
end
def next_step
self.current_step = steps[steps.index(current_step)+1]
end
def previous_step
self.current_step = steps[steps.index(current_step)-1]
end
def last_step?
current_step == steps.last
end
end
class McqsController < ApplicationController
def new
session[:mcq_params] ||= {}
#quiz = Quiz.find(params[:quiz_id])
#mcq = Mcq.new(session[:mcq_params])
#mcq.current_step = session[:mcq_step]
end
def create
#quiz = Quiz.find(params[:quiz_id])
session[:mcq_params].deep_merge!(params[:mcq]) if params[:mcq]
#mcq = Mcq.new(session[:mcq_params])
#option_count = session[:mcq_params]['option_count']
#option_count.times { #mcq.options.build }
#mcq.quiz_id = params[:quiz_id]
#mcq.current_step = session[:mcq_step]
if params[:previous_button]
#mcq.previous_step
elsif #mcq.last_step?
#mcq.save if #mcq.valid?
else
#mcq.next_step
end
session[:mcq_step] = #mcq.current_step
if #mcq.new_record?
render "new"
else
session[:mcq_step] = session[:mcq_params] = nil
redirect_to edit_quiz_path(#mcq.quiz_id)
end
end
end
But each time the second page is shown, the no of fields for options doubles or in case of invalid entry only the field for question_text is shown. How do I show the options correctly? Should I just go with my first solution? I'm new to rails and don't know much about the best practices.
Edited :
new.html.erb
<div class="sub-heading">Add a Multiple Choice Question:</div>
<%= render "mcq_#{#mcq.current_step}", quiz: #quiz, mcq: #mcq %>
_mcq_setup.html.erb
<div class="form-container">
<%= form_for [quiz, mcq] do |f| %>
<div class="form-row">
<div class="response-count">How many options should the question have?</div>
<%= f.select(:option_count, (2..5)) %>
</div>
<div class="form-row">
<div class="response-count">How many options can be selected?</div>
<div class="option">
<%= f.radio_button :multiselect, 'false', checked: true %>
<%= f.label :multiselect, 'Just One', value: 'false' %>
</div>
<div class="option">
<%= f.radio_button :multiselect, 'true' %>
<%= f.label :multiselect, 'Multiple', value: 'true' %>
</div>
</div>
<div class="form-row">
<div class="response-count">Is the question required?</div>
<div class="option">
<%= f.radio_button :required, 'true', checked: true %>
<%= f.label :required, 'Yes', value: 'true' %>
</div>
<div class="option">
<%= f.radio_button :required, 'false' %>
<%= f.label :required, 'No', value: 'false' %>
</div>
</div>
<%= f.submit "Continue to the Next Step" %>
<% end %>
</div>
_mcq_options.html.erb
<%= form_for [quiz, mcq] do |f| %>
<%= f.label :question_text, 'What is your question?' %>
<%= f.text_field :question_text %>
<%= f.fields_for :options do |option_fields| %>
<%= option_fields.label :option_text, "Option #{option_fields.options[:child_index] + 1}:" %>
<%= option_fields.text_field :option_text %>
<% end %>
<%= f.hidden_field :multiselect %>
<%= f.hidden_field :required %>
<%= f.submit "Add Question" %>
<%= f.submit "Back to previous step", name: 'previous_button' %>
<% end %>
You may look in direction of state_machine. By using that you can use your steps as states of state machine and use its ability to define validations that only active for given states (look here in state :first_gear, :second_gear do) so fields that required on second step will be not required on first. Also, it'll allow you to avoid complex checks for the step you currently on (because the state will be persisted in a model) and will be pretty easy to extend with more steps in a future.

Survey App Using Nested Models, Partials, & Rails Form Builder

I extracted this code from the following engine:
Link to Demo:
https://rapidfire.herokuapp.com/rapidfire/question_groups/1110/answer_groups/new
Link to Code:
https://github.com/code-mancers/rapidfire/blob/master/app/services/rapidfire/answer_group_builder.rb
I trying to modify it so that I can create custom answer types, called "kind"(s) (see schema). One kind called "indicator" (see _indicator.html.erb), has multiple columns.
My models are setup as follows:
Survey
has_many :question_groups
accepts_nested_attributes_for :question_groups
end
QuestionGroup
belongs_to :survey
has_many :questions
accepts_nested_attributes_for :questions
end
AnswerGroup
belongs_to :survey
has_many :answers
accepts_nested_attributes_for :answers
end
Answer
belongs_to :question
belongs_to :answer_group, inverse_of: :answers
end
Question 1: What does this line do? (see build_answer_group method)
#answers = question_group.questions.collect do |question|
In the console,
#answers = [#<Answer id: nil, answer_group_id: nil, question_id: 1034,
answer_text: nil, created_at: nil, updated_at: nil>, #<Answer id: nil,
answer_group_id: nil, question_id: 1035, answer_text: nil, created_at: nil,
updated_at: nil>]
question_ids 1034 and 1035 refer to the last items in the array produced by
question_group.questions
Instead, I need #answers to be the Answer class equivalent of all questions.
Why is it returning only two items?
Question 2:
For the indicator partial, I need to display the name of the Question Group and the names of each subitem, e.g. "Questions", and five additional columns with unique html names and ids. So, if I had two question groups of kind "indicator", I need to produce something like:
QuestionGroup1.name
id = answer_group_1034_answer_text name = answer_group[1034][answer_text]
id = answer_group_1035_answer_text name = answer_group[1035][answer_text]
etc...
QuestionGroup2.name
id = answer_group_1036_answer_text name = answer_group[1036][answer_text]
id = answer_group_1037_answer_text name = answer_group[1037][answer_text]
Currently, it is producing:
For QuestionGroup1:
<textarea rows="5" name="answer_group[1034][answer_text]"
id="answer_group_1034_answer_text"></textarea>
<textarea rows="5" name="answer_group[1034][answer_text]"
id="answer_group_1034_answer_text"></textarea>
For QuestionGroup2:
<textarea rows="5" name="answer_group[1035][answer_text]"
id="answer_group_1034_answer_text"></textarea>
<textarea rows="5" name="answer_group[1035][answer_text]"
id="answer_group_1034_answer_text"></textarea>
Goal: Assign a unique id and name for each row within a question group for the indicator partial
views/answer_groups/_fidelitychecklist.html.erb
<%= form_for([#survey, #answer_group_builder]) do |f| %>
<% #answer_group_builder.answers.each do |answer| %>
<%= f.fields_for("#{answer.question.id}", answer) do |answer_form| %>
<%= render_answer_form_helper(answer, answer_form) %>
<% end %>
<% end %>
<%= f.submit "Save" %>
<% end %>
services/answer_group_builder.rb
class AnswerGroupBuilder < BaseService
attr_accessor :user, :survey, :questions, :answers, :params
def initialize(params = {})
super(params)
build_answer_group
end
def to_model
#answer_group
end
...
private
def build_answer_group
#answer_group = AnswerGroup.new(user: user, survey: survey)
#survey.question_groups.each do |question_group|
#answers = question_group.questions.collect do |question|
#answer_group.answers.build(question_id: question.id)
end
end
end
AnswerGroupsController
before_filter :find_survey!
def new
#answer_group_builder = AnswerGroupBuilder.new(answer_group_params)
end
def create
#answer_group_builder = AnswerGroupBuilder.new(answer_group_params)
if #answer_group_builder.save
redirect_to surveys_path
else
render :new
end
end
private
def find_survey!
#survey = Survey.find(params[:survey_id])
end
def answer_group_params
answer_params = { params: params[:answer_group] }
answer_params.merge(user: current_user, survey: #survey)
end
module ApplicationHelper
def render_answer_form_helper(answer, form)
partial = Question.where(id: answer.question_id).last.question_group.kind.to_s
questiongroup = Question.find(answer.question_id).question_group
render partial: "/answers/#{partial}", locals: {f: form, answer: answer, questiongroup: questiongroup }
end
views/answers/_indicator.html.erb
<%= render partial: "answers/errors", locals: {answer: answer} %>
<fieldset>
<table class="indicator">
<thead>
<th class="first head-row">Indicators</th>
<th class="head-row">Presence</th>
<th class="head-row">Rating</th>
<th class="head-row">Element(s) Implemented Well</th>
<th class="head-row">Element(s) Requiring Further Support</th>
<th class="head-row">Comments/Action Steps</th>
</thead>
<tbody>
<tr><td colspan="6" class="indicator-title"><%= questiongroup.name %></td></tr>
<% questiongroup.questions.each do |question| %>
<tr>
<td class="first row">
<%= question.question_text %>
</td>
<td class="row">
<%= f.select :answer_text, options_for_select([["N/O", "n/o"], ["N", "n"]]) %>
</td>
<td class="row">
<%= f.select :answer_text, options_for_select([["1", "1"], ["2", "2"], ["3", "3"]]) %>
</td>
<td class="row">
<%= f.text_area :answer_text, rows: 5 %>
</td>
<td class="row">
<%= f.text_area :answer_text, rows: 5 %>
</td>
<td class="row">
<%= f.text_area :answer_text, rows: 5 %>
</td>
</tr>
<% end %>
</tbody>
</table>
</fieldset>
schema.rb
create_table "surveys", force: :cascade do |t|
t.string "name", limit: 255
end
create_table "question_groups", force: :cascade do |t|
t.string "name", limit: 255
t.integer "position", limit: 4
t.integer "survey_id", limit: 4
t.text "kind", limit: 65535
end
create_table "questions", force: :cascade do |t|
t.string "question_text", limit: 255
t.integer "position", limit: 4
t.integer "question_group_id", limit: 4
end
create_table "answer_groups", force: :cascade do |t|
t.integer "survey_id", limit: 4
end
create_table "answers", force: :cascade do |t|
t.integer "answer_group_id", limit: 4
t.integer "question_id", limit: 4
t.text "answer_text", limit: 65535
end
routes.rb
resources :surveys do
resources :answer_groups, only: [:new, :create] do
end
end

Updating two tables with one to many relationship via form

I am trying to update my two tables through my form but I am getting this error:
Started POST "/test" for ::1 at 2014-12-02 00:15:21 -0800
Processing by UsersController#create as JS
Parameters: {"utf8"=>"✓", "users"=>{"first_name"=>"sdfdsfdsf", "last_name"=>"dsfdsfds", "email"=>"3213213#hotmail.com", "phone_number"=>"23123213", "message"=>"sdfdsfsdfdsfkjsdfksdfk;adklsfjksadfksjdfklsdf"}, "commit"=>"Save Users"}
Completed 500 Internal Server Error in 1ms
ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/users_controller.rb:32:in `create'
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (2.6ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.text.erb (0.4ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.6ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.text.erb (12.2ms)
I have been reading examples and documentation and I think I wrote my form correctly but I think my controller is missing some aspects because I am new to rails.
Here is my schema:
ActiveRecord::Schema.define(version: 20141130075753) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "contact_requests", force: true do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "message", null: false
t.integer "user_id"
end
add_index "contact_requests", ["user_id"], name: "index_contact_requests_on_user_id", using: :btree
create_table "users", force: true do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "email", null: false
t.string "phone_number"
end
end
Models:
class User < ActiveRecord::Base
has_many :contact_requests
validates(:first_name, presence: true)
validates(:last_name, presence: true)
validates(:email, presence: true)
accepts_nested_attributes_for :contact_requests
end
class ContactRequest < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :message, presence: true, length: { maximum: 500 }
end
user controller:
def create
if !(User.find_by(email: params[:users][:email]))
#user = User.new(user_params)
#contact_request = ContactRequest.new(contact_request_params)
#contact_request.save
#user.save
else
#contact_request = ContactRequest.new(contact_request_params)
#contact_request.save
end
end
private
def user_params
# strong_parameters, which requires us to tell Rails exactly which parameters
# we want to accept in our controllers
params.require(:users).permit(:first_name, :last_name, :email, :phone_number)
end
end
form:
<!-- contact -->
<section id="contact">
<div class="container">
<div class="title-container">Contact Us</div>
<div class="title-caption">Reach us at (415)-911-9999</div>
<%= form_for(:users, remote: true, id: "contact-form", class: "contact-input") do |f| %>
<div class="col-md-12">
<div class="col-md-6">
<div class="contact-input-margin form-group">
<%= f.text_field(:first_name, class: "form-control", placeholder: "First name")%>
</div>
<div class="contact-input-margin form-group">
<%= f.text_field(:last_name, class: "form-control", placeholder: "Last name") %>
</div>
<div class="contact-input-margin form-group">
<%= f.email_field(:email, class: "form-control", placeholder: "Email") %>
</div>
<div class="contact-input-margin form-group">
<%= f.telephone_field(:phone_number, class: "form-control", placeholder: "Phone number") %>
</div>
</div>
<div class="contact-input-margin col-md-6">
<div class="form-group">
<%= f.fields_for :contact_requests do |builder| %>
<%= f.text_area(:message, class: "form-control contact-margin", rows: "8", placeholder: "Message...") %>
<% end %>
</div>
</div>
</div>
<%= f.submit(class: "btn btn-xl") %>
<% end %>
</div>
</section>

Resources