Determine if the form is sent to the tracks with a ruby?

Is there a way to determine if a form has been submitted? I am trying to set a class based on user check as below, maybe?

.control-group{ :class => ("error" if form_is_submitted ) } 

Now try:

  .control-group{ :class => ("error" if params[:user][:profile_attributes][:gender] == nil) } 

This will fail if the form is not submitted because the parameters are zeros and cause an error.

+4
source share
1 answer

If your form data is sent via fields with name attributes of type user[profile_attributes][gender] (all have the user prefix), you can check if :user exists in params .

 ... if params.include?(:user) 

If for any reason (for example, coming from the route) params[:user] will already matter even for GET requests, you can look for a specific form field that has a value. For example, you can add a hidden field

 <%= f.hidden_field :some_field, :value => true %> 

and check it in your condition

 ... if params[:user].include?(:some_field) 

You can also check if the request passed using the POST method

 ... if request.post? 

Does this work for other methods like request.put? for the update method.

+15
source

All Articles