In my rails app I'm making a guess at the user's gender based on some information that the user enters on the 'new' form. After that the user is taken to the show page, where I reveal the guess. I want the user to tell me if I was right or wrong so i can improve the data. I want to have 'yes' and 'no' buttons where 'no' updates the user's record in the db with the other gender.
Here's my show view
<div class='container'>
<header>We think you're.....</header>
<div class='cmn-t-shake'>
<%= @person.male ? "Male" : "Female"%>
</div>
<div>
Were we right? <br/>
<%= link_to 'Yes', '/people'%>
|
<%= link_to 'No', 'people/flip_gender(@person)'%>
</div>
<div>
<%= link_to 'Learn more', '/people'%>
</div>
</div>
and a snippet of my controller
class PeopleController < ApplicationController
def index
@men = Person.where("male = ?", true)
@women = Person.where("male = ?", false)
end
def new
@person = Person.new
end
def show
@person = Person.find(params[:id])
end
def create
@person = Person.new(person_params)
@person.male = guessGender(@person)
if @person.save
path = '/people/' + @person.id.to_s
redirect_to path
else
render 'new'
end
end
def person_params
params.require(:person).permit(:male, :height_in_inches, :weight_in_lbs)
end
def flip_gender(p)
p = Person.find(params[:id])
person.gender = !p.flip_gender
p.save
end
and my routes.rb
root 'people#new'
get 'people' => 'people#index'
get 'people/new' => 'people#new'
post 'people' => 'people#create'
get 'people/:id' => 'people#show'
How can I user flipGender to update and save the current record on the show page at the click of a button/link?