Reputation: 139
I'm using the admin session to create a new client. When I submit the form I need it to redirect to that client show page but the @client id is equal to nil at that point. I'm using devise for the authentication, so when the admin creates the client it goes directly to the client homepage. I need to go to the "Admin" client index. Thanks for your help.
<%= form_for @client, :url => client_path(@client) do |f| %>
<p> <%= f.label :name, "Empresa" %> <%= f.text_field :name %></p>
<p> <%= f.label :email %> <%= f.text_field :email %> </p>
<p> <%= f.label :password %> <%= f.text_field :password %> </p>
<p> <%= f.label :contact %> <%= f.text_field :contact %></p>
<%= f.submit "Create", :class => "btn btn-primary" %>
Upvotes: 1
Views: 1157
Reputation: 76784
"Admin" client index.
If it's namespaced, you'll need to deal with the namespace in all of your object & path references:
<%= form_for [:admin, @client] do |f| %> #-> sends to admin/clients#create
This assumes you're using a namespaced Admin::ClientsController
controller:
#config/routes.rb
namespace :admin do
resources :clients, only: [:new, :create, :index]
end
#app/controllers/admin/clients_controller.rb
class Admin::ClientsController < ApplicationController
before_action :authenticate_user!
def index
@clients = Client.all
end
def new
@client = Client.new
end
def create
@client = Client.new client_params
@client.save
end
end
As an aside, you need to ensure you're not using HTML as a styling mechanism.
I see people using <p>
and <br />
for styling all the time; it's wrong. You have an entire CSS pipeline to style your forms:
#app/assets/stylesheets/application.css
form input {
margin: 10px 0;
}
You can see this in action here: http://jsfiddle.net/qgk4dy3j/
Upvotes: 1
Reputation: 53
Do you create a new client instance on clients_controller.rb method new ?
also on your form I believe that <%= form_for @client do |f| %>
is sufficient
Example:
def new
@client = Client.new
end
def create
@client = Client.new(client_params)
if @client.save
redirect_to @client
else
render :new
end
end
Upvotes: 0