I might be approaching this wrong but I have a model called Location
, a location is a name... a group_id
(from the model Group
) and an address_id
(from Address
Model). The flow is:
user selects a group to which a location is to be added. The page where user can add a location, has the group previously selected, an input box for the name of the location and a list of available addresses, PLUS the ability to add a new address (which would take you to a different page, rendered by new action of a controller corresponding to the Address
model). I have to do other things similar for other models too. So figuring this out would help a lot. The part that trips me up is:
I am sure I could figure out how to get the select box of addresses to work and I would have the group so I would have a group id, and the address id and then just create the GroupLocation with the name entered.
The problem is, when a new address is to be added, because the form is present in a different page (as mentioned above), upon submission of the address, how does the controller know that besides creating an address, the created address will ultimately be used for the selected group for a new location.
You can pass the group_id
as a hidden form field which is pre-populated. When the form is submitted, extract that hidden field value in your controller, and after creating the address, pass it to next controller (locations controller) as a query parameter through a redirect.
addresses_controller.rb
class AddressesController < ApplicationController
def new
@address = Address.new
@group_id = params[:group_id] # This should be provided either as a route parameter or a query parameter.
end
def create
@address = Address.create! permitted_address_params
# the group_id parameter is obtained from hidden field of form
# which will be passed to the locations controller as a query
# parameter along with the id of newly created address.
redirect_to new_location_path(address_id: @address.id, group_id: params[:group_id])
end
private
def permitted_address_params
params.require(:address).permit(...fields from address ...)
end
end
locations_controller.rb
class LocationsController < ApplicationController
def new
@location = Location.new(address_id: params[:address_id], group_id: params[:group_id])
end
def create
@location = Location.create! permitted_location_params
end
private
def permitted_location_params
params.require(:location).permit(:group_id, :address_id, ...other location params ....)
end
end
addresses/new.html.erb
<%= form_for @address do |f| %>
<%= hidden_field_tag :group_id, @group_id %>
... other address fields ...
<% end %>
locations/new.html.erb
<%= form_for @location do |f| %>
<%# This ensures that group_id and address_id are passed to the create action of locations controller %>
<%= f.hidden_field :group_id %>
<%= f.hidden_field :address_id %>
... other location fields ...
<% end %>
I hope this points you in the right direction. If you still have doubts please ask in the comments.
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.