If I have a model named Client and controller that looks like this
class ClientController < ApplicationController
def new
@client = Client.new
@client.name = "New Guy"
end
end
and I am using HAML, what will my view need to look like to render a form based on this.
I have this
%p= @client.name
-form_for :client, @client do |c|
c.label :c.name
c.text_field :c.name
but my html ends up looking like this
<p>New Guy</p>
<form accept-charset="UTF-8" action="/client/new" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="orGtE1V05QIaye5kSsAi5SBdEAV0AJX9uZYUzHw5P64=" /></div>
c.label :c.name
c.text_field :c.name
</form>
Update: Thanks for the answers. when I change the view to
%p= @client.name
-form_for :client, @client do |c|
= c.label :c.name
= c.text_field :c.name
I get the following error
undefined method `name' for :c:Symbol
Extracted source (around line #3):
1: %p= @client.name
2: -form_for :client, @client do |c|
3: = c.label :c.name
4: = c.text_field :c.name
If I comment out lines 2,3, and 4 The name prints out fine. What am I missing?
Update 2:
If I change the view to
%p= @client.name
-form_for :client, @client do |c|
c.label :name
c.text_field :nam开发者_Python百科e
no more error. Thanks.
You need to use =
before the ruby function calls, so that they're evaluated.
%p= @client.name
-form_for :client, @client do |c|
= c.label :c.name
= c.text_field :c.name
This is what you want:
%p= @client.name
- form_for :client, @client do |c|
= c.label :c.name
= c.text_field :c.name
I think it's this:
%p= @client.name
-form_for :client, @client do |c|
=c.label :c.name
=c.text_field :c.name
This doesn't take into account any visual formatting of course...
精彩评论