FlowMafia
FlowMafia

Reputation: 1002

Nothing happens after clicking submit button (Ruby on Rails)

I'm doing a form to register new users in my rails app, I used the inspect params method to see what is it sending in the form but when I click the submit button nothing happens, looking up for solutions I found nothing helpful and I don't know what to do

register.html.erb in views/register/register.html.erb

    <%= form_with model: @client, url: clients_path do |f| %>
        <div class="form-group  row"><label class="col-sm-2 col-form-label">*Name</label>
            <div class="col-sm-3">
               <%= f.text_field :name, class:"form-control" %>
            </div>

            <label class="col-sm-2 col-form-label">*Last name</label>
                <div class="col-sm-3">
                    <%= f.text_field :last_name, class:"form-control"%>
                </div>
       </div>

      <!-- Buttons -->
          <div class="hr-line-dashed"></div>
              <div class="form-group row">
                  <div class="col-sm-4 col-sm-offset-2">
                      <%= f.submit "Continue", class:"btn btn-primary btn-sm"%>
                      <%= f.submit "Cancel", class:"btn btn-primary btn-sm"%>
                   </div>
               </div>
     <% end %>
</div>

routes.rb

  root to: "mainpages#index"

  get '/mainpages', controller: 'mainpages', action: 'index'
  get '/register', controller: 'register', action: 'register'

  resources :clients

clients_controller.rb

class ClientsController < ApplicationController
  def create
    render plain: params[:client].inspect
  end    
end

client.rb (model)

class Client < ApplicationRecord
end

Upvotes: 0

Views: 638

Answers (3)

Sammy360
Sammy360

Reputation: 171

By default, form_with sets data-remote to "true", to render something else after submission. Try and set local to "true":

<%= form_with model: @client, url: clients_path, local: true do |f| %>
  
   ..

<% end %>

Upvotes: 0

Violeta
Violeta

Reputation: 710

What I think you're missing is an instance of the class Client which you can solve it in two ways:

  1. In your register controller, create the instance of the Client class, like this:
class RegisterController < ApplicationController
  def register
    @client = Client.new
  end
end

OR

  1. In your form:
<%= form_with model: Client.new do |f| %>
...
<% end %>

Also, keep in mind, in your clients_controller, you need to save the new object/record:

class ClientsController < ApplicationController
  def create
    @client = Client.new(client_params)

    if @client.save
      redirect_to @client, notice: 'Client was successfully created.'
    else
      render :new  #or redirect somewhere else
    end
  end

  private

  def client_params
    params.require(:client).permit(:name, :last_name)
  end
end

Upvotes: 0

demir
demir

Reputation: 4709

remote attribute of form_with is true by default. Therefore, nothing happens after clicking submit. You need to set remote to false:

<%= form_with model: @client, url: clients_path, remote: false do |f| %>
  ..
<% end %>

Upvotes: 1

Related Questions