Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get simple_form to use the base class for urls and not the subclass?

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?

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.

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]".

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.

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

base_class => 'User'

That would handle all of these things.

like image 412
Fire Emblem Avatar asked Nov 28 '22 18:11

Fire Emblem


1 Answers

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

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]).

like image 78
sefnap Avatar answered Dec 07 '22 00:12

sefnap