railsの学習(CONTROLLERS MUST BE EATEN)

続いて、Level 4 のTHE VIEW AIN'T ALWAYS PRETTYです。

Level 4 Challenges

Resources
zombies

id name graveyard
1 Ash Glen Haven Memorial Cemetary
2 Bob Chapel Hill Cemetary
3 Jim My Fathers Basement

params = { :id => 1 }

/zombies/show.html.erb
<%= @zombie.name %>

問題1 Show Action

Create the show action for a Zombies Controller which finds a Zombie based on params[:id].

解答例


問題2 Respond To

Finish the respond_to block so the action returns xml format

解答例

Resources
zombies

id name graveyard
1 Ash Glen Haven Memorial Cemetary
2 Bob Chapel Hill Cemetary
3 Jim My Fathers Basement

params = { :zombie => {:name => 'Gregg', :graveyard => 'TBA'} }

問題3 Controller Create Action

Write a create action that will create a new Zombie from the params and then redirect to the created zombie's show page

解答例

Resources
zombies

id name graveyard
1 Ash Glen Haven Memorial Cemetary
2 Bob Chapel Hill Cemetary
3 Jim My Fathers Basement

tweets

id status zombie_id
1 Where can I get a good bite to eat? 1
2 My left arm is missing, but I don't care 2
3 I just ate some delicious brains 3
4 OMG, my fingers turned green. #FML 1

params = { :id => 4 }

/zombies/show.html.erb
<%= @zombie.name %>

問題4 Controller Before Filter

Add a before filter which calls a method to check if a zombie has no tweets. Redirect to zombies_path if they dont have tweets. Only on show.

解答例