你如何让simple_form使用urls的基类而不是子类?

时间:2021-03-01 15:57:05

Whenever I use simple_form_for(), it defaults to "my_subclasses_path" rather than "my_base_classes_path". How can I tell it to not use the subclass?

每当我使用simple_form_for()时,它默认为“my_subclasses_path”而不是“my_base_classes_path”。我怎么能告诉它不要使用子类?

In my case, I have a User object, but also many subclasses. I want to use the standard user urls as all the subclasses work the same for these forms.

在我的例子中,我有一个User对象,但也有很多子类。我想使用标准用户URL,因为所有子类对这些表单的工作方式相同。

In addition, I want to stop simple_form from naming the properties after the subclass. For example, if the subclass is "Admin", I want the submitted parameters to be "params[:user]" and not "params[:admin]".

另外,我想阻止simple_form在子类之后命名属性。例如,如果子类是“Admin”,我希望提交的参数是“params [:user]”而不是“params [:admin]”。

I find the above to be REALLY odd because the form actually says "user[first_name]" and NOT "admin[first_name]" - but simple_form seems to want to make it params[:admin] anyway.

我发现上面的内容真的很奇怪,因为表单实际上是“user [first_name]”而不是“admin [first_name]” - 但是simple_form似乎想让它成为params [:admin]。

Basically, subclasses are causing simple_for to not behave properly. I wish there was an option like

基本上,子类会导致simple_for无法正常运行。我希望有一个选项

base_class => 'User'

That would handle all of these things.

这将处理所有这些事情。

2 个解决方案

#1


4  

Pass the :url option to simple_form_for.

将:url选项传递给simple_form_for。

eg.: simple_form_for(@admin, :url => users_path)

例如:simple_form_for(@admin,:url => users_path)

#2


19  

If you pass in the :url option as rafaelfranca suggests, along with the :as option, it should work correctly. For example:

如果您传入:url选项,如rafaelfranca建议,以及:as选项,它应该正常工作。例如:

simple_form_for(@admin, :url => user_path(@admin), :as => :user)

This gives me the correct form action and parameters (e.g. user[name] instead of admin[name]).

这为我提供了正确的表单操作和参数(例如user [name]而不是admin [name])。

#1


4  

Pass the :url option to simple_form_for.

将:url选项传递给simple_form_for。

eg.: simple_form_for(@admin, :url => users_path)

例如:simple_form_for(@admin,:url => users_path)

#2


19  

If you pass in the :url option as rafaelfranca suggests, along with the :as option, it should work correctly. For example:

如果您传入:url选项,如rafaelfranca建议,以及:as选项,它应该正常工作。例如:

simple_form_for(@admin, :url => user_path(@admin), :as => :user)

This gives me the correct form action and parameters (e.g. user[name] instead of admin[name]).

这为我提供了正确的表单操作和参数(例如user [name]而不是admin [name])。