1

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?

Veg
  • 556
  • 2
  • 8
  • 24

1 Answers1

2

You could make flipGender a helper method which should make it accessible from the view:

def flip_gender(p)
    p = Person.find(params[:id]) 
    person.gender = !p.flip_gender
    p.save
end

helper_method: flipGender

and then in your view:

<% flipGender(p) %>

or something like that. You could also create a file for helper methods, and then include it in the ApplicationController so those methods will be available check out this thread

Community
  • 1
  • 1
HolyMoly
  • 2,020
  • 3
  • 22
  • 35
  • awesome! do you know how i can call it conditionally when my link is clicked? – Veg Sep 24 '15 at 01:15
  • one thought is make it a button_to which hits that action in the controller, and then from there maybe redirect. it seems based your current code that you want to direct the user somewhere anyways when they hit the yes/no link? here is some info on button_to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to – HolyMoly Sep 24 '15 at 01:35
  • you can also try to use jquery or something to put an event listener on the elements when they are clicked. which at that point, you could use the helper. i am not sure if it will work since it is a ruby helper, but i am thinking it might since it is now available on the front end in a view. i'm still learning myself ;) – HolyMoly Sep 24 '15 at 01:38
  • 1
    Keep learning :) Just one small thing, in Ruby, we usually define method in this way: `some_method` rather `someMethod` :) Checkout this Ruby Style Guide: https://github.com/bbatsov/ruby-style-guide for more style related best practices. And yes, I am learning too. I guess everybody is learning :) – K M Rakibul Islam Sep 26 '15 at 05:18