StartupInstituteChicago / Spring2014HerbPreintz

0 stars 0 forks source link

Question #3

Open hpreinitz opened 10 years ago

hpreinitz commented 10 years ago

<%= form_for :restaurant , url: restaurant_path, method: :patch do |f| %> I don't understand the preceding line.

sibrent commented 10 years ago

Nice!

Brent Williams @brentskeez Program Manager Chicago, Startup Institute

Apply for our Summer Classes http://apply.startupinstitute.com/ Forbeshttp://www.forbes.com/sites/karstenstrauss/2013/03/04/raising-an-army-of-startup-employees/ | ReadWritehttp://readwrite.com/2013/03/04/boston-startup-school-becomes-startup-institute-expands-to-new-york | GigaOMhttp://gigaom.com/2013/03/04/startups-101-new-program-provides-8-week-institute-for-wannabe-tech-employees/ | Xconomyhttp://www.xconomy.com/boston/2013/03/04/boston-startup-school-adds-ny-gets-new-name-startup-institute/

On Tue, Mar 25, 2014 at 4:35 PM, hpreinitz notifications@github.com wrote:

<%= form_for :restaurant , url: restaurant_path, method: :patch do |f| %> I don't understand the preceding line.

Reply to this email directly or view it on GitHubhttps://github.com/StartupInstituteChicago/Spring2014HerbPreintz/issues/3 .

jkelleyj commented 10 years ago

Hey Herb - That line is a helper provided as part of the rails framework. Let's break it down a bit.

The <%= %> wrapper means you're outputting content to the generated HTML.

The method w/ explicit parens is really this:

form_for(:restaurant, { url: restaurant_path(), method: :patch }) do |f|
    # form inputs and contents in here
end 

the first parameter, restaurant is a symbol used by the method to create names for all the form inputs. Since you're creating a form to update the restaurant, it'll name all the input parameters things like params[:restaurant][:name] in the parameters you get after posting the form. These appear in html like:

The form itself gets generated as a tag w/ a method and action (standard HTML form stuff)...the URL and method you are passing as a hash drive these.

<form method="PATCH" action="/restaurant/1234">
</form>

restaurant_path is a URL helper provided by rails routing: http://guides.rubyonrails.org/routing.html#path-and-url-helpers

Calling it here, just provides the proper path to your restaurant patch path.

Given that you are posting to update a restaurant, you probably need your form_for to be taking a variable that you setup in the controller:

<%= form_for @restaurant, url: restaurant_path(@restaurant), method: :patch do |f| %>

In this case, I think the URL parameter would be redundant, as rails helpers will create the proper path when you pass restaurant in the form. This is a bit of rails magic where it figures out the path based on whether restaurant is new/existing.

Lastly, the do |f| part is opening a block where the form helper is available as the variable f. This lets you generate the form HTML like so:

<%= form_for @restaurant, url: restaurant_path(@restaurant), method: :patch do |f| %>
  <%= f.text_field :name %>
<% end %>

That will create a text input inside the form HTML.

Hope that helps explain it a bit.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html