Skip to content

Instantly share code, notes, and snippets.

@pier-oliviert
Last active December 18, 2015 07:09
Show Gist options
  • Save pier-oliviert/5744512 to your computer and use it in GitHub Desktop.
Save pier-oliviert/5744512 to your computer and use it in GitHub Desktop.
class ElectionsController < ApplicationController
def index
@elections = Election.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @elections }
end
end
def new
@election = Election.new
respond_to do |format|
format.html #new.html.erb
format.json {render :json => @election}
end
end
def show
@election = Election.find(params[:id])
respond_to do |format|
format.html #show.html.erb
format.json {render :json =>@election}
end
end
def create
@election = Election.new(params[:election])
respond_to do |format|
if @election.save
format.html { redirect_to @election, notice: 'Election was successfully created.' }
format.json { render json: @election, status: :created, location: @election }
else
format.html { render action: "new" }
format.json { render json: @election.errors, status: :unprocessable_entity }
end
end
end
def edit
@election = Election.find(params[:id])
end
def update
@election = Election.find(params[:id])
params[:election][:candidates] = params[:election].fetch(:candidate, []).map { |id| Candidate.find(id) }
respond_to do |format|
if @election.update_attributes(params[:election])
format.html {redirect_to(@election,
:notice => 'Election was successfully updated.')}
format.json {head :no_content}
else
format.html {render :action=>'edit'}
format.json {render :json => @election.errors,
:status => :unprocessable_entity}
end
end
def destroy
@election = Election.find(params[:id])
@election.destroy
respond_to do |format|
format.html {redirect_to elections_url}
format.json {head :no_content}
end
end
end
end
NoMethodError in ElectionsController#update
undefined method `each' for "#<Candidate:0x000000046fb310>":String
Rails.root: /home/jonblack/projects/mcet
Application Trace | Framework Trace | Full Trace
app/controllers/elections_controller.rb:50:in `block in update'
app/controllers/elections_controller.rb:49:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"G+rEHO99U7mGhETIyphwXMCuYnmXPpH3GC8fE+KcfhM=",
"election"=>{"candidates"=>"#<Candidate:0x000000046fb310>",
"location"=>"Haiti",
"year"=>"2010",
"round1_votes"=>"",
"round2_votes"=>"",
"round3_votes"=>"",
"position"=>"President"},
"commit"=>"Update Election",
"id"=>"1"}
Show session dump
Show env dump
Response
Headers:
None
<%= form_for(@election) do |f| %>
<% if @election.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@election.errors.count, "error") %> prohibited this user
from being saved:</h2>
<ul>
<% @election.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<h2>Candidates in the Race</h2>
<%= f.collection_select("candidates[]", Candidate.all, :id, :first_name) %>
<h2>Election Information</h2>
<%= f.label :location %><br/>
<%= f.text_field :location %>
<%= f.label :year %><br/>
<%= f.text_field :year %>
<%= f.label :round1_votes %><br/>
<%= f.text_field :round1_votes %>
<%= f.label :round2_votes %><br/>
<%= f.text_field :round2_votes %>
<%= f.label :round3_votes %><br/>
<%= f.text_field :round3_votes %>
<%= f.label :position %><br/>
<%= f.text_field :position %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Election < ActiveRecord::Base
attr_accessible :location, :position, :round1_votes, :round2_votes, :round3_votes, :year, :candidates
has_many :candidates
accepts_nested_attributes_for :candidates
end
class Candidate < ActiveRecord::Base
attr_accessible :birthday, :criminal_history, :description, :education, :first_name, :last_name, :location, :party, :position, :professional_history, :election_id
belongs_to :election
def name_with_initial
"#{first_name.first}. #{last_name}"
end
end
<select id="election_candidates" name="election[candidates]"><option value="#&lt;Candidate:0x00000004605f28&gt;">#&lt;Candidate:0x00000004605f28&gt;</option>
<option value="#&lt;Candidate:0x000000046fb310&gt;">#&lt;Candidate:0x000000046fb310&gt;</option>
<option value="#&lt;Candidate:0x000000046facd0&gt;">#&lt;Candidate:0x000000046facd0&gt;</option>
<option value="#&lt;Candidate:0x000000046fa730&gt;">#&lt;Candidate:0x000000046fa730&gt;</option></select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment