Last active
December 18, 2015 07:09
-
-
Save pier-oliviert/5744512 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select id="election_candidates" name="election[candidates]"><option value="#<Candidate:0x00000004605f28>">#<Candidate:0x00000004605f28></option> | |
<option value="#<Candidate:0x000000046fb310>">#<Candidate:0x000000046fb310></option> | |
<option value="#<Candidate:0x000000046facd0>">#<Candidate:0x000000046facd0></option> | |
<option value="#<Candidate:0x000000046fa730>">#<Candidate:0x000000046fa730></option></select> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment