How to make a string into an object instance name?

text_field(“post”, “title”, “size” => 20)
will generate:

so, when you name “post”, there is a way to translate this word to
@post”, an object’s name. how can I do that?

and i saw:
def link_to_page(page_name, web = @web, text = nil, options = {})

end

in a helper file.
what does “web = @web” do here?

Nanyang Z. wrote:

text_field(“post”, “title”, “size” => 20)
will generate:

so, when you name “post”, there is a way to translate this word to
@post”, an object’s name. how can I do that?

In your view you can do this with:
object_name = “post”
instance_variable_get("@#{object_name}")

However, most of the time you should use @post directly.

Dan M.

Nanyang Z. wrote:

and i saw:
def link_to_page(page_name, web = @web, text = nil, options = {})

end

in a helper file.
what does “web = @web” do here?

It means if “web” is not passed in to the link_to_page method, it
defaults to the value of @web.

link_to_page(:page_name) #=> uses @web
link_to_page(:page_name, :my_web) #=> uses :my_web

Dan M.

Dan M. wrote:

what does “web = @web” do here?

It means if “web” is not passed in to the link_to_page method, it
defaults to the value of @web.

Thanks Dan.

What about my first question?
I defined a helper method, namely address_field(object_name)
I used it in a view file like address_field(“school”)
now I want to use @school.city inside this method, how can I do that?
I don’t want to put @school.city inside directly, because this helper
method will be used by other objects.

Dan M. wrote:

In your view you can do this with:
object_name = “post”
instance_variable_get("@#{object_name}")

Dan M.

I am sorry, I replied before I saw your first post.

I added
@object = instance_variable_get("@#{object_name}")
in to the method, I can use @object.city now.

Thanks again!