Beginner of rails here, for my project I need to create a client with things such as first name, middle name, last name and their passport. I have another table called passports that will store the passport. I want to be able to add a passport number in association with the client. Here are the relevant pieces of code:
Controller for Client
def new
@client = Client.new
end
def create
@client = Client.new(client_params)
if @client.save
redirect_to root_url
else
render 'new'
end
end
private
def client_params
params.require(:client).permit(:firstname, :middlename, :lastname, :PresentAddress)
end
end
Controller for Passport
def new
@passport = Passport.new
end
def create
passport = Passport.new(passport_params)
if @passport.save
flash[:notice] = "Passport saved"
else
flash[:notice] = "Passport not saved"
end
end
private
def passport_params
params.require(:passport).permit(:idnumber, :country)
end
end
Model for Client
has_one :passport, foreign_key: "client_id"
Model for Passport
belongs_to :client
Migration file for client
create_table :clients do |t|
t.string :firstname
t.string :middlename
t.string :lastname
end
Migration file for passport
create_table :passport do |t|
t.string :passport
end
/clients/_form.html.erb
<%= form_for(@client) do |f| %>
<%= f.label :"First Name"%>
<%= f.text_field :firstname%>
<%= f.label :"Middle Name"%>
<%= f.text_field :middlename%>
<%= f.label :"Last Name"%>
<%= f.text_field :lastname%>
<%= f.submit "Create Client" %>
<% end %>
/passport/_form.html.erb
<%= form_for([@client, @client.passport.build]) do |f| %>
<%= f.label :"Passport ID Number" %>
<%= f.text_field :idnumber %>
<% f.submit "Create Passport" %>
<% end %>
/clients/new.html.erb
<%= render "clients/form" %>
<%= render "passports/new" %>
I want to be able to create a passport for the client in this form and send it to my Postgres database. How would I go about doing that? Please let me know if I'm missing any other information.
EDIT - changed to a field that's more simple for now. Also modified to do what ChiefrockaChris suggested.
The way to do this would be to put your #_form.html.erb view for addresses under your address views folder and then to render it as a partial in your client#create, client#update etc. view. So for example:
#clients/_form.html.erb
<%= form_for(@client) do |f| %>
<%= f.label :"First Name"%>
<%= f.text_field :FirstName%>
<%= f.label :"Middle Name"%>
<%= f.text_field :MiddleName%>
<%= f.label :"Last Name"%>
<%= f.text_field :LastName%>
<%= f.label :"Present address"%>
<%= f.text_field :PresentAddress %>
<%= f.submit "Create Client" %>
<% end %>
Then in your address views folder
#addresses/_form.html.erb
<%= form_for ([@client, @client.addresses.build]) do |f| %>
<%= f.label :"Street Name"%>
<%= f.text_field :street_name%>
<%= f.submit "Create Address" %>
<% end %>
Then on client#new:
#client/new.html.erb
<%= render "clients/form" %>
<%= render "addresses/form" %>
Also be sure that there is something in your address#create method that will reference the user creating so it saves in the database as such. Also please do not capitalize your attribute names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.