Can't multiple destroy with checkboxes - ruby-on-rails

I need to multiple delete tasks with checkboxes, when i do this have an error
Have tasks and checkboxes for every task, when clicked on checkbox and then click the "Delete selected" button it must be deleted all checked tasks
NoMethodError in TasksController#delete_multiple
undefined method `destroy'
here is my request
{"utf8"=>"✓",
"_method"=>"delete",
"authenticity_token"=>"Bc2lZKUDVOjkQ0DYTDNI8TVliMaDKb+z2wz46RJeFqFol8WyEABA8sAz+WPCQOD2V0SEyqSHAryuoYQ6nvk4sA==",
"cb_tasks"=>["1", "3", "4"],
"commit"=>"Delete selected"}
and
my task_controller
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
# multiple delete with checkboxes
def delete_multiple
#tasks = Task.find(params[:cb_tasks])
#tasks.destroy() // **here is a problem**
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Tasks was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:title, :description, :priority, :due, :done)
end
end
my task.rb
class Task < ApplicationRecord
def destroy
Task.find(params[:cb_tasks]).destroy
flash[:success] = "Material destroyed."
redirect_to tasks_url
end
end
my index.html.rb
<%= form_tag delete_multiple_tasks_path, method: :delete do %>
<div class="CSSTableGenerator" >
<table >
<tr>
<td>Tasks</td>
</tr>
<% #tasks.each do |task| %>
<tr>
<td><%= check_box_tag "cb_tasks[]", task.id %></td>
<td><%= link_to task.title, task %></td>-->
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: {confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
</table>
</div>
<%= submit_tag "Delete selected" %>
<% end %>
my routes
resources :tasks do
collection do
delete 'delete_multiple'
end
end
Why it can't undefined method `destroy' ?
Can anyone help me?

The problem is below line
#tasks = Task.find(params[:cb_tasks])
#tasks.destroy() // **here is a problem**
You can modify this like below
Task.where(id: params[:cb_tasks]).destroy_all
I think will help

instead of overwrite destroy method in model side you can do this and i would suggest you not to overwrite destroy however if you overwrite destroy method than you are not passing params[:cb_tasks] to this method and in Task.find(params[:cb_tasks]).destroy this line fails to execute because it does not get params[:cb_tasks] here in model
so you can do this like -
def delete_multiple
#tasks = Task.find(params[:cb_tasks])
#tasks.destroy_all // **replace it**
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Tasks was successfully destroyed.' }
format.json { head :no_content }
end
end

I find how it just works. I deleted method destroy the model
and in controller do this
def delete_multiple
Task.where(id: params[:cb_tasks]).destroy_all
end
Thanks for all your answers, you are the best!

Related

controller isn't updating values in the database when using the .increment method

I have a rails controller that isn't updating the value in the database when I call the .increment! method.
The funny thing is, the same code works in the create method
Here's the controller code
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
#requests = Request.all
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def new
#request = Request.new
end
# GET /requests/1/edit
def edit
end
# POST /requests
# POST /requests.json
def create
#request = Request.new(request_params)
respond_to do |format|
if #request.save
format.html { redirect_to #request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: #request }
#request.increment!(:voteCount)
else
format.html { render :new }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
respond_to do |format|
if #request.update(request_params)
format.html { redirect_to #request, notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: #request }
else
format.html { render :edit }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
#request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
#request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:artist, :title, :voteCount)
end
def upvote
puts 'upvote'
request = Request.find(params[:request_id])
render json: { voteCount: request.voteCount }
#request.increment!(:voteCount)
request.increment!(:voteCount)
request.save
request.reload
end
end
Here's the html
<tbody>
<% #requests.each do |request| %>
<tr data-request-id="<%= request.id %>">
<td id="artist" style="text-align: center"><%= request.artist %></td>
<td id="songTitle" style="text-align: center"><%= request.title %></td>
<td style="text-align: center" id="voteCount<%= request.id %>"><%= request.voteCount %></td>
<td style="text-align: center"><%= button_to 'Vote', request_upvote_path(request.id), remote: true, method: :post, onclick: "upvote(#{request.id})", class: 'upvote', id:"voteButton#{request.id}" %></td> </tr>
<% end %>
Here's the JS that updates the count on the page
$("#ClickMe").attr("disabled", "disabled");
function upvote(id) {
var count = document.getElementById("voteCount" + id).innerHTML;
count = parseInt(count);
count = count + 1;
count = count.toString();
document.getElementById("voteCount" + id).innerHTML = count;
document.getElementById("voteButton" + id).disabled = true;
}
Adding routes.rb
Rails.application.routes.draw do
resources :requests do
post 'upvote', to: 'requests#upvote'
end
end
I have 2 question before answer your question
Did that code return any error, while you run it?
Why do you have two increment! method?
You have #requests in your ERB template which is problem because you don't have #requests instance variable in your controller.
.increment! is correct method for voting; however, you need to save it because
Only attribute is updated; the record itself is not saved.
So, you need to save your record to database
def upvote
#request = Request.find(params[:request_id])
#request.increment!(:voteCount)
#request.save
render json: { voteCount: #request.voteCount }
end
Possible links about the topic:
Ruby on Rails: strange voting increment behavior
Ruby on Rails Increment Counter in Model
How do I implement voting in Rails

Rails : How to accessing relation attributes from the view

I have the following rails code
Employee model: id | emp_name | emp_number
class Employee < ActiveRecord::Base
has_many :visitors
end
Visitor Model:id|employee_id|visitor_name|vis_company|vis|email
class Visitor < ActiveRecord::Base
belongs_to :employee
end
Employee Controller :
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to #employee, notice: 'Employee was successfully created.' }
format.json { render :show, status: :created, location: #employee }
else
format.html { render :new }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to #employee, notice: 'Employee was successfully updated.' }
format.json { render :show, status: :ok, location: #employee }
else
format.html { render :edit }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:emp_id, :emp_name, :emp_email, :emp_phone, :emp_mobile)
end
end
Visitor Controller :
class VisitorsController < ApplicationController
before_action :set_visitor, only: [:show, :edit, :update, :destroy]
# GET /visitors
# GET /visitors.json
def index
##visitors = Visitor.find(:all, :order => 'emp_name')
##visitors = Visitor.all.includes(:emp_name)
#visitors = Visitor.all
##employees = #visitors.Employee.find(:all, :order => 'emp_name')
##employees = #visitors.employee :include => [:emp_name]
end
# GET /visitors/1
# GET /visitors/1.json
def show
end
# GET /visitors/new
def new
#visitor = Visitor.new
end
# GET /visitors/1/edit
def edit
end
# POST /visitors
# POST /visitors.json
def create
#visitor = Visitor.new(visitor_params)
respond_to do |format|
if #visitor.save
format.html { redirect_to #visitor, notice: 'Visitor was successfully created.' }
format.json { render :show, status: :created, location: #visitor }
else
format.html { render :new }
format.json { render json: #visitor.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /visitors/1
# PATCH/PUT /visitors/1.json
def update
respond_to do |format|
if #visitor.update(visitor_params)
format.html { redirect_to #visitor, notice: 'Visitor was successfully updated.' }
format.json { render :show, status: :ok, location: #visitor }
else
format.html { render :edit }
format.json { render json: #visitor.errors, status: :unprocessable_entity }
end
end
end
# DELETE /visitors/1
# DELETE /visitors/1.json
def destroy
#visitor.destroy
respond_to do |format|
format.html { redirect_to visitors_url, notice: 'Visitor was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_visitor
#visitor = Visitor.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def visitor_params
params.require(:visitor).permit(:vis_id, :vis_name, :vis_email, :vis_company, :employee_id)
end
end
Now my main problem is that I cant access employee name from visitor view :
<p id="notice"><%= notice %></p>
<h1>Listing Visitors</h1>
<table>
<thead>
<tr>
<th>Vis</th>
<th>Vis name</th>
<th>Vis email</th>
<th>Vis company</th>
<th>Visitor Host</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #visitors.each do |visitor| %>
<tr>
<td><%= visitor.id %></td>
<td><%= visitor.vis_name %></td>
<td><%= visitor.vis_email %></td>
<td><%= visitor.vis_company %></td>
<td><%= visitor.employee.emp_name %></td>
<td><%= link_to 'Show', visitor %></td>
<td><%= link_to 'Edit', edit_visitor_path(visitor) %></td>
<td><%= link_to 'Destroy', visitor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Visitor', new_visitor_path %>
I created new project from the scratch and for some reason it started working. My mistake was that I didn't define the relation from the start. I added employee_id after everything else is created I think the rails didn't build the relation at that time. thanks everyone

updating cart via button

I'm trying to be on that page right before the checkout, where you can modify the quantity and update all the prices accordingly. Now my "Update" button, updates 'something' but it's not what i'm trying to achieve, it sends me on a line_items show page that i don't even want to have (i'm keeping it for now to avoid the missing template error). The rails documentation isn't helping me ... well anyway, the code is this
<ul>
<% #cart.line_items.each do |item| %>
<%= form_for(item) do |f| %>
<li><%= f.number_field :quantity, :value => item.quantity %> x <%= item.product.name %>
<%= item.total_price %> </li>
<% end %>
<%= submit_tag "Update" %>
<% end %>
<br><strong>Total Price:</strong> <%= #cart.total_price %>
</ul>
<%= button_to "Checkout", new_order_path, method: :get, data: { confirm: "Are you sure?" } %>
<%= button_to 'Empty cart', #cart, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'Back', categories_path %>
Thank you !
edited
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create, :update, :destroy]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
#line_items = LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
#line_item = LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product.id)
respond_to do |format|
if #line_item.save
format.html { redirect_to #line_item.cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: #line_item }
else
format.html { render :new }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
def update
respond_to do |format|
if #line_item.update(line_item_params)
format.html { redirect_to #cart, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: #line_item }
else
format.html { render :edit }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
#line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id, :order_id)
end
end
Just do redirect after you update an item. And provide correct line_item_update_params method that permites quantity param. Your controller should look like that:
class ItemsController < ApplicationController
def update
item = Item.find(params[:id])
item.update(line_item_update_params)
redirect_to :back # this is the key line of code
end
...
private
# it is used for creating
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id, :order_id)
end
# it is used for updating
def line_item_update_params
params.require(:line_item).permit(:quantity)
end
...
end

before_filter for nested resource

I have nested resources - users have_many manufacturers and manufacturers have_many lines. I wrote a before_filter to load the #manufacturer so it could go through the rest of the functions in the lines_controller. Problem is, I'm running into issues when I click edit on the lines/show view.
Error: Couldn't find Manufacturer with id=manufacturer_id
77 def load_manufacturer
78 #manufacturer = Manufacturer.find(params[:manufacturer_id])
79 end
So here's what I'm working with:
app/views/lines/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #line.name %>
</p>
<p>
<strong>Manufacturer:</strong>
<%= #line.manufacturer_id %>
</p>
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,#line) %> |
<%= link_to 'Back', manufacturer_lines_path(#line.manufacturer_id) %>
lines_controller.rb
class LinesController < ApplicationController
before_action :set_line, only: [:show, :edit, :update, :destroy]
before_filter :load_manufacturer
# GET /lines
# GET /lines.json
def index
#lines = Line.all
end
# GET /lines/1
# GET /lines/1.json
def show
end
# GET /lines/new
def new
#manufacturer = Manufacturer.find(params[:manufacturer_id])
#line = #manufacturer.lines.build
end
# GET /lines/1/edit
def edit
end
# POST /lines
# POST /lines.json
def create
#line = #manufacturer.lines.build(line_params)
respond_to do |format|
if #line.save
format.html { redirect_to manufacturer_line_path(#manufacturer, #line), notice: 'Line was successfully created.' }
format.json { render action: 'show', status: :created, location: #line }
else
format.html { render action: 'new' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lines/1
# PATCH/PUT /lines/1.json
def update
respond_to do |format|
if #line.update(line_params)
format.html { redirect_to manufacturer_line_path(#manufacturer, #line), notice: 'Line was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lines/1
# DELETE /lines/1.json
def destroy
#line.destroy
respond_to do |format|
format.html { redirect_to manufacturer_lines_url(#manufacturer) }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line
#line = Line.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_params
params.require(:line).permit(:name, :manufacturer_id)
end
def load_manufacturer
#manufacturer = Manufacturer.find(params[:manufacturer_id])
end
end
This:
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,#line) %>
Should be
<%= link_to 'Edit', edit_manufacturer_line_path(#manufacturer,#line) %>
What is happening is it is changing the symbol :manufacturer_id to a string and passing it to the route so your route looks like
`/manufacturers/manufacturer_id/lines/###/edit`
when you want
/manufacturers/###/lines/###/edit

Couldn't find Generator without an ID

I'm trying to get the results of all generators and results merge them into 1 table.
I have the following association :
Generator has_many: results
Result belongs_to:generators
Whenever i want to create a new results , i got an error saying couldn't find Generator without an ID. Why do i get the error ? How should i fix it ?
For example : localhost:3000/generators/new = is the part where i entered my values into the generator form and after clicking create button , it'll bring me to localhost:3000/generators/8/results/new. It is here after keying in values for this form , i get the error message saying Couldn't find Generator without an ID . I'm trying to have a page whereby i can display both Generators + Result values together. SHow all generators and their respective results data in 1 page.
GeneratorController
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
# GET /generators
# GET /generators.json
def index
#generators = Generator.all(:include => [:results])
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
#generator.choice = params[:choice]
if params[:choice] == 'Randomly'
#generator.random_generate(generator_params)
elsif params[:choice] == 'No_of_ATGC'
#generator.no_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
elsif params[:choice] == 'Seating'
#generator.seating(params[:user_seq])
end
#generator.result_choice=params[:result_choice]
respond_to do |format|
if #generator.save
if #generator.result_choice == 'Yes'
format.html { redirect_to(new_generator_result_path(#generator)) }
else
format.html { redirect_to #generator, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
end
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
private
def set_generator
#generator = Generator.find(params[:id])
end
def generator_params
params.require(:generator).permit(:primer_length,:choice,:random_primer_generated,:no_A,:no_T,:no_G,:no_C,:user_seq)
end
end
Result.rb
class Result < ActiveRecord::Base
attr_accessible :ncbi_ref_seq,:genome_seq, :genome_sample
belongs_to :generator, foreign_key: "generator_id"
def generate_result(result_params)
ref_seq = self.ncbi_ref_seq
Bio::NCBI.default_email = "spykix#hotmail.com"
fasta_sequence = Bio::NCBI::REST::EFetch.nucleotide(ref_seq,"fasta")
fasta=Bio::FastaFormat.new(fasta_sequence)
self.genome_seq = fasta.data
self.genome_sample = fasta.definition
g=Generator.last
p=self.genome_seq.scan(g.c_primer)
self.binding_times= p.length()
end
end
ResultController [ the error is highlighting at the line for def create ; generator = Generator.find(params[:id]) ]
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
# GET /results
# GET /results.json
def index
#results = Result.all
end
# GET /results/1
# GET /results/1.json
def show
end
# GET /results/new
def new
#result = Result.new
end
# GET /results/1/edit
def edit
end
# POST /results
# POST /results.json
def create
#result = Result.new
#result = #result.generate_result(result_params)
generator = Generator.find(params[:id])
#result = generator.results.build(result_params)
# generator = Generator.find(3)
# #result = generator.results.build(result_params)
# #result=#result.generate_result(result_params)
respond_to do |format|
if #result.save
format.html { redirect_to #result, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #result }
else
format.html { render action: 'new' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /results/1
# PATCH/PUT /results/1.json
def update
respond_to do |format|
if #result.update(result_params)
format.html { redirect_to #result, notice: 'Result was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
# DELETE /results/1
# DELETE /results/1.json
def destroy
#result.destroy
respond_to do |format|
format.html { redirect_to results_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_result
#result = Result.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def result_params
params.require(:result).permit(:ncbi_ref_seq)
end
end
generator/index.html.erb
<h1>[Index]Random Amplified Polymorphic DNA [RAPD] Primer Generator</h1>
<table>
<thead>
<tr>
<th>Primer length</th>
<th>Selected choice</th>
<th>Random primer generated</th>
<th>Complimentary primer</th>
<th>NCBI ref seq</th>
<th>Genome sample</th>
<th>Binding times</th>
</tr>
</thead>
<tbody>
<% #generators.each do |generator| %>
<tr>
<td><%= generator.primer_length %></td>
<td><%= generator.choice %></td>
<td><%= generator.random_primer_generated %></td>
<td><%= generator.c_primer %></td>
<% for result in generator.results %>
<td><%= result.ncbi_ref_seq %></td>
<td><%= result.genome_sample %></td>
<td><%= result.binding_times %></td>
<%end%>
<td><%= link_to 'Show', generator %></td>
<td><%= link_to 'Edit', edit_generator_path(generator) %></td>
<td><%= link_to 'Destroy', generator, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'Home', root_path %>
<%= link_to 'New Generator', new_generator_path %>
If generator have many results, than First you should find generator in new method, because when you create any record, it initializes from the new method. I am rewriting you new and create methods, you can take reference from it:
def new
generator = Generator.find(params[:id])
#result = generator.results.build(result_params)
end
# POST /results
# POST /results.json
def create
#result = #result.generate_result(result_params)
respond_to do |format|
if #result.save
format.html { redirect_to #result, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #result }
else
format.html { render action: 'new' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
Hope this may help.

Resources