Reputation: 1002
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
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
Reputation: 710
What I think you're missing is an instance of the class Client
which you can solve it in two ways:
register controller
, create the instance of the Client
class, like this:class RegisterController < ApplicationController
def register
@client = Client.new
end
end
OR
<%= 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
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