I create application for making routes with rails 7 stimulus. First question: How to use in form only available location? Idk and i think using wrong methods like("#{Location.first.title}") in form.select. How to get available locations by another way? Second: My form create a new location but i don't want it. All location must be created separate. From another form. I think something redundant. May be a add redundant references.
locations_controller.rb
...
def location_params
params.require(:location).permit(:user_id, :title, :comment, :description, :address, :country, :city, :state, :image, :short_discription, :latitude, :longitude)
end
tours_controller.rb
...
def tour_params
params.require(:tour).permit(:name, :comment, :description, :user_id, :location, locations_attributes: %i[user_id title address _destroy comment])
end
models/location.rb
belongs_to :user
belongs_to :tour
model/tour.rb
has_many :locations, dependent: :destroy, inverse_of: :tour
accepts_nested_attributes_for :locations, allow_destroy: true, reject_if: :all_blank
tours/_form.html.haml
= form_for @tour, data: {controller: "nested-form", nested_form_wrapper_selector_value: '.nested-form-wrapper'} do |f|
- if @tour.errors.any?
#error_explanation
%h2= "#{pluralize(@tour.errors.count, "error")} prohibited this tour from being saved:"
%ul
- @tour.errors.full_messages.each do |message|
%li= message
.field
= f.label :name
= f.text_field :name
.field
= f.hidden_field :user_id, value: "#{current_user.id}"
.field
= f.label :description
= f.text_area :description
%template(data-nested-form-target="template")
= f.fields_for :locations, Location.new, child_index: "NEW_RECORD" do |location_fields|
= render "tours/tour_location_form", f: location_fields
= f.fields_for :locations do |location_fields|
= render "tours/tour_location_form", f: location_fields
/ Inserted elements will be injected before that target.
%div(data-nested-form-target="target")
%button(data-action="nested-form#add" type="button") Add location
.actions
= f.submit 'Save'
tours/tour_location_form.html.haml
.nested-form-wrapper(data-new-record="#{f.object.new_record?}")
= f.label :title
= f.select :title, ["#{Location.first.title}", "#{Location.second.title}", "#{Location.find(6).title}"]
= f.label :comment
= f.text_field :comment
= f.hidden_field :user_id, value: current_user.id
%button(data-action="nested-form#remove" type="button") Remove location
= f.hidden_field :_destroy
routes.rb
...
resources :locations
resources :tours
schema.rb
create_table "locations", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "address"
t.string "country"
t.string "city"
t.string "state"
t.string "image"
t.text "short_discription"
t.float "latitude"
t.float "longitude"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "tour_id"
t.string "comment"
t.bigint "user_id", null: false
t.index ["tour_id"], name: "index_locations_on_tour_id"
t.index ["user_id"], name: "index_locations_on_user_id"
end
create_table "tours", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "location"
t.bigint "user_id"
t.string "comment"
t.index ["user_id"], name: "index_tours_on_user_id"
end