Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

form for - Ruby on Rails : symbol as argument in form_for

I understand what is passed to the form_for method when doing something like :

<% form_for(@user) do |f| %> ... <% end %>

if @user is set in the controller. This is pretty obvious.

But what happens when we pass :user, as I have seen in many examples ?

<% form_for(:user) do |f| %> ... <% end %>

When should I use the symbol version ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using the symbol will generate:

<form action="/users" method="post">

Using the object @user set to new you get:

<form action="/users" class="new_user" id="new_user" method="post">

If you set @user to something else it will change the form tag generated, of course ... try it out for yourself.

In the end it all depends what you're trying to do. You might want to use the symbol if you have multiple :user forms on the same page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...