Why is my simple_fields_for not iterating - ruby-on-rails-4

I have the following (simplified) classes:
class CandidateList < ActiveRecord::Base
has_many :candidates, inverse_of: :candidate_list, dependent: :destroy
...
end
and
class Candidate < ActiveRecord::Base
belongs_to :candidate_list, inverse_of: :candidates
...
end
In my controller, I create a CandidateList with 10 Candidate objects and querying the database verifies that they exist and are attached.
In my form, I've got the following:
<%= simple_form_for save_bingo_terms_path( candidate_list_id: #candidate_list ) do |f| %>
...
<%= #candidate_list.candidates.count %>
<table>
<%= f.simple_fields_for :candidates do |cf| %>
<tr>
<td><%= cf.input :name, label: false %></td>
<td><%= cf.input :definition, label: false %></td>
</tr>
<% end %>
<% end %>
Can anyone help me understand why it only shows me a single row in the table with an input field for name and definition even though the count output above the table shows 10?
Thanks in advance!

:candidates is not associated with the #candidate_list object, it's associated with a save_bingo_terms_path
Change the simple_form_for to
<%= simple_form_for #candidate_list do |f| %>
And don't forget to specify...
class CandidateList < ActiveRecord::Base
has_many :candidates, inverse_of: :candidate_list, dependent: :destroy
accepts_nested_attributes_for :candidates
...
end
Alternatively you can explicitly specify an each loop and reference each candidate
<% #candidate_list.candidates each do |candidate| %>
<% f.simple_fields_for :candidates, candidate do |cf| %>
<tr>
<td><%= cf.input :name, label: false %></td>
<td><%= cf.input :definition, label: false %></td>
</tr>
<% end %>
<% end %>

Related

Creating new record through fields for within Rails 4

I'm attempting to create a new oil production within the production information page. The fields are appearing but I am unable to get the oil production to save. Any help is appreciated
Productioninfos_controller.rb
def index
#productioninfos = Productioninfo.all
end
def new
#productioninfo = Productioninfo.new
end
def productioninfo_params
params.require(:productioninfo).permit(:firstproduction, :lastproduction, :numberofcompl, :totaldepth, :productionzone, oilproductioninfos_attributes: [:id, :oilcum, :avgbpmlastsixmonths, :avgbpodlastsixmonths])
end
Productioninfo.rb (model)
has_many :oilproductioninfos
accepts_nested_attributes_for :oilproductioninfo, :allow_destroy => :true, :reject_if => :blank
Oilproductioninfo.rb (model)
belongs_to :productioninfo
Productioninfos_form.html.erb
<%= f.fields_for :oilproductioninfos do |ff| %>
<div>
<%= ff.label :oilcum %>
<%= ff.text_field :oilcum %>
<%= ff.label :avgbpmlastsixmonths %>
<%= ff.text_field :avgbpmlastsixmonths %>
<%= ff.label :avgbpodlastsixmonths %>
<%= ff.text_field :avgbpodlastsixmonths %>
</div>

Nested attributes text field isn't showing up in my views

I am new to the rails community. I am working on an application where users can have username with a nested attribute of first and last name. The other text field associated with the User model works fine.
Any help would be much appreciated.
Attached are the app models, controllers, migration files, db schema, and views.
models
class User < ActiveRecord::Base
has_one :username, dependent: :destroy
accepts_nested_attributes_for :username, allow_destroy: true
end
class Username < ActiveRecord::Base
belongs_to :user
end
Migrations
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :about_me
t.string :nationality
t.string :sexe
t.timestamps null: false
end
end
end
class CreateUsernames < ActiveRecord::Migration
def change
create_table :usernames do |t|
add_column :username, :first_name, :string
add_column :username, :last_name, :string
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
controller
class UsersController < ApplicationController
def index
#all_users = User.all
#new_user = User.new
#username = Username.new
end
def new
#new_user = User.new
end
def create
User.create(user_params)
end
private
def user_params
params.require(:user).permit(:email, :about_me, username_attributes:
[:last_name, :first_name])
end
end
Views
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<%= form_for #new_user do |f| %>
<%= f.fields_for #new_user do |user| %>
<div class="field">
<%= user.label :email%>
<%= user.text_field :email %>
<%= user.label :about_me %>
<%= user.text_field :about_me %>
</div>
<% end %>
<%= f.fields_for :username do |name| %>
<div class="field">
<%= name.label :first_name %>
<%= name.text_field :first_name %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Try this
controller
def new
#new_user = User.new
#new_user.usernames.build
end
view
<%= form_for #new_user do |f| %>
# ...
<%= f.fields_for :usernames do |name| %>
<div class="field">
<%= name.label :first_name %>
<%= name.text_field :first_name %>
</div>
<% end %>
# ...
<% end %>
helper
def name_format(user_mst)
name = user_mst.first_name.capitalize
name += (" #{user_mst.last_name.capitalize}")
return name
end
to display on view

many to many association mass assign checkboxes ruby on rails 4

what i want to accomplish it's almost exactly the same as this!
railscast tutorial but can't get it to work.I have a courses table and a enrollments table they have a many to many relation so i have a join table called "courses_enrollments" i'd like to be able to add many courses to enrollments all at once from a form... i used a has_many_through relation and looped through all the courses in the form and for each of them i have a checkbox so i can add them to enrollments but when i save nothing happens my join table (courses_enrollments) never gets filled with the relations i want.... here are my models:
courses:
class Course < ActiveRecord::Base
has_many :courses_enrollments
has_many :enrollments, :through => :courses_enrollments
enrollments:
class Enrollment < ActiveRecord::Base
has_many :courses_enrollments
has_many :courses, :through => :courses_enrollments
and courses_enrollments:
class CoursesEnrollment < ActiveRecord::Base
belongs_to :courses
belongs_to :enrollments
end
and finally my enrollments form view:
<%= form_for(#enrollment) do |f| %>
<% if #enrollment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#enrollment.errors.count, "error") %> prohibited this enrollment from being saved:</h2>
<ul>
<% #enrollment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :state %><br>
<%= f.collection_select(:state, Courses.all, :id, :name, {:include_blank => true}, {:multiple => false}) %>
</div>
I also tried it like this in the form.... but nothing gets saved here either
<% Courses.all.each do |course|%>
<%= check_box_tag "enrollment[course_id][]", course.id %>
<%= course.name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
i want to be able to save many records to the join table that holds the relation between the enrollments i am creating or updating and the courses i selected with the has many through association.... appreciate the help
Models:
class Course < ActiveRecord::Base
has_and_belongs_to_many :enrollments
class Enrollment < ActiveRecord::Base
has_and_belongs_to_many :courses
with this type of association you end up with a join table which will have course_id and enrollment_id . Thus not needing to create a new model just to handle your association. Rails already does that for you.
After that, in your controllers, make sure the accepted_params have this:
Controllers:
def course_params
params.require(:course).permit(
...
enrollments_ids: []
def enrollment_params
params.require(:enrollment).permit(
...
courses_ids: []

Why doesn't article.tag_list display the tags?

I have added gem 'acts-as-taggable-on' -v 2.3.1 in Gemfile.
Rails version 3.2.13, Ruby -v 1.9.3
In article.rb,
class Article < ActiveRecord::Base
acts_as_taggable_on :tags
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
In articles_controller.rb,
class ArticlesController < ApplicationController
before_filter :authenticate_author!, except: [:index, :show]
def index
myarray = Article.all
#articles = Kaminari.paginate_array(myarray).page(params[:page]).per(10)
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
if #article.update_attributes(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :tag_list => [])
end
end
In articles/_form.html.erb
<div class="field">
<%= f.label :tag_list, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
I ran the following commands after installing the acts-on-taggable-gem,
rails generate acts_as_taggable_on:migration
rake db:migrate
In articles/index.html.erb
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article.tag_list %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>
The tags are not being displayed in the index page.
But if I do ,
article = Article.new(:title => "Awesome")
article.tag_list = "awesome, cool"
article.save
The transaction gets committed and the tags are displayed on the browser.
Why aren't the tags getting saved and displayed in the index page?
The Tag List is an array, which means you need to 'loop' to extract the data. Whenever I have used acts_as_taggable I run through a loop to extract the tags. Something like the following should work:
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td>
<% article.tag_list.each do |tag| %>
<%= tag %>
<% end %>
</td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>

View loop with nested params

Working on the Domain view. I would like to loop through and list all Domains, all controls and any goals that are under those controls. I cannot figure out how to loop through the controls goals. Current solution just loops through all the goals, not just the goals that we connected to the Control
class Domain < ActiveRecord::Base
has_and_belongs_to_many :controls
class Control < ActiveRecord::Base
has_and_belongs_to_many :domains
has_many :goals
class Goal < ActiveRecord::Base
belongs_to :control
def index #domain controller
#domains = Domain.all
#controls = Control.all
#goals = Goal.all
end
domain index view
<% #domains.each do |domain| %>
<tr>
<td><%= domain.domainName %></td>
<% domain.controls.each do |control| %>
<td><%= control.controlName %></td>
<% #goals.each do |t| %>
<td>SG<%= t.goalsNumber %>, </td>
<% end %>
<% end %>
This is what I came up with. Probably not the most efficient way to do this but it works for now
<% domain.controls.each do |control| %>
<td>
<h3><%= control.controlName %></h3>
</td>
</tr>
<% #goals.each do |goals| %>
<% if goals.control.controlName==c ontrol.controlName %>
<tr>
<td>SG
<%=g oals.goalsNumber %>
<%=g oals.goalsName %>
</td>
</tr>
<% else %>
<% end %>
<% end %>
<% end %>