Simple_form_for: Removing the root model name from parameters

When I create a form using simple_form_for @model, after submitting post, params has all the attributes grouped by [model] parameters. How do I get simple_form to remove this group and send it directly instead (at the root of params)?

<%= simple_form_for @user, do |f| %> <%= f.input :name %> <%= f.input :password %> <%= f.submit %> 

Now the name and password attributes are usually sent under the parameters [: user] [: name], params [: user] [: password], etc. How to get simple_form to publish them as params [: name], params [: password] etc.?

Thanks!
Ramkumar

ps: If you are wondering why I need this, the main part of my application will serve as an API, and I created a set of methods for checking the request, which expects some attributes to be at the root. In the rare case (forgot password) I really need to submit a form, and I'm looking for a way to use these methods.

+4
source share
2 answers

Two ways I can think of:

First, do not use simple_form to create your form, but do it manually or using the form_tag and *_tag . This will allow you to more accurately determine which parameters are used in your form.

If you want to save simple_form , then ask it to call another controller action. Refactor controllers to split the logic into a separate method. Sort of:

 class UsersController def create_from_api controller_logic(params) end def create_from_form controller_logic(params[:user]) end def controller_logic(params) [actual work happens here] end end 
+1
source

you can explicitly define a name for input by passing input_html to it:

  input_html: { name: :name } 

(I needed this to send the resource to a third-party endpoint with a redirect to my side, which was based on the names of simple attributes, but I really did not want to create a shortcut and enter through tags;))

also see the simplest form constructor

+1
source

All Articles