Guest User

Untitled

a guest
May 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. Here are some explanations on "form_for" :
  2.  
  3.  
  4. How does `form_for` works:
  5.  
  6. ```erb
  7. <%= form_for(@pet) do |f| %>
  8. ...
  9. <% end %>
  10. ```
  11.  
  12. Will generate the following HTML:
  13.  
  14. ```erb
  15. <form class="new_pet" id="new_pet" action="/pets" method="post">
  16. </form>
  17. ```
  18.  
  19. So `form_for` takes an object and from this object, infers if it should be an update or a create and sets the URL (action attribute in the form) to the right thing, same for the method.
  20. For instance for a create the url should be "/pets" and the method should be POST.
  21. For an update, the url should be "/pets/1" and the method should be PATCH.
  22.  
  23. How does `form_for` does that, here is what the code looks like ?
  24.  
  25. ```ruby
  26. @pet = Pet.new
  27.  
  28.  
  29. # The process is as follow:
  30. @pet.class # => Pet
  31. @pet.class.to_s # => "Pet"
  32. @pet.class.to_s.downcase # => "pet"
  33. @pet.class.to_s.downcase.pluralize # => "pets"
  34.  
  35. # Therefore the code is:
  36. if @pet.id == nil # Then it is a create
  37. url = "/" + @pet.class.to_s.downcase.pluralize
  38. # => /pets
  39. method = "POST"
  40. else # Then this is an update
  41. url = "/" + @pet.class.to_s.downcase.pluralize + "/" + @pet.id
  42. # => /pets/1
  43. method = "PATCH"
  44. end
  45. ```
  46.  
  47. Et Voila.
  48.  
  49.  
  50. Same goes for a nested form even though it is a bit more complex:
  51. Provided that:
  52.  
  53. ```ruby
  54. @restaurant = Restaurant.find(params[:restaurant_id])
  55. @review = Review.new
  56. ```
  57.  
  58. ```erb
  59. <%= form_for([@restaurant, @review]) do |f| %>
  60. ...
  61. <% end %>
  62. ```
  63.  
  64. Will generate the following HTML:
  65.  
  66. ```erb
  67. <form class="new_restaurant_review" id="new_restaurant_review" action="/restaurants/1/reviews" method="post">
  68. </form>
  69. ```
  70.  
  71. How does `form_for` does that ?
  72.  
  73. ```ruby
  74. @restaurant = Restaurant.find(params[:restaurant_id])
  75. @review = Review.new
  76.  
  77. # The process is as follow:
  78. @restaurant.class # => Restaurant
  79. @restaurant.class.to_s # => "Restaurant"
  80. @restaurant.class.to_s.downcase # => "restaurant"
  81. @restaurant.class.to_s.downcase.pluralize # => "restaurants"
  82.  
  83. # The process is as follow:
  84. @review.class # => Review
  85. @review.class.to_s # => "Review"
  86. @review.class.to_s.downcase # => "review"
  87. @review.class.to_s.downcase.pluralize # => "reviews"
  88.  
  89. # Then the url is pretty much generated like that:
  90. "#{@restaurant.class.to_s.downcase.pluralize}/#{@restaurant.id}/#{@review.class.to_s.downcase.pluralize}"
  91.  
  92. # And this gives you: /restaurants/1/reviews
  93. ```
Add Comment
Please, Sign In to add comment