我有问题,我实现动态选择,但不更新ciudades。
有人占用了这段代码吗?我的代码。
autos.coffee:
$ ->
$(document).on 'change', '#regiones_select', (evt) ->
$.ajax 'update_ciudades',
type: 'GET'
dataType: 'script'
data: {
region_id: $("#regiones_select option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic region select OK!")
汽车控制器:
class AutosController < ApplicationController
before_action :set_auto, only: [:show, :edit, :update, :destroy]
def index
@autos = Auto.all
end
def show
end
def new
@regiones = Region.all
@ciudades = Ciudad.where("region_id = ?", Region.first.id)
@auto = Auto.new
end
def edit
end
def create
@auto = Auto.new(auto_params)
respond_to do |format|
if @auto.save
format.html { redirect_to @auto, notice: 'Auto was successfully created.' }
format.json { render :show, status: :created, location: @auto }
else
format.html { render :new }
format.json { render json: @auto.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @auto.update(auto_params)
format.html { redirect_to @auto, notice: 'Auto was successfully updated.' }
format.json { render :show, status: :ok, location: @auto }
else
format.html { render :edit }
format.json { render json: @auto.errors, status: :unprocessable_entity }
end
end
end
def destroy
@auto.destroy
respond_to do |format|
format.html { redirect_to autos_url, notice: 'Auto was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_auto
@auto = Auto.find(params[:id])
end
def auto_params
params.require(:auto).permit(:region_id, :ciudad_id,)
end
end
_form.html.erb:
<%= form_for @auto, url: {action: "create"}, html: {method: "post"} do |f| %>
<%= f.select :region_id, options_for_select(@regiones.collect { |region|
[region.region.titleize, region.id] }, 1), {}, { id: 'regiones_select' } %>
<%= f.select :ciudad_id, options_for_select(@ciudades.collect { |ciudad|
[ciudad.ciudad.titleize, ciudad.id] }, 0), {}, { id: 'ciudades_select' } %>
<%= f.submit "Go!" %>
<% end %>
new.html.erb:
<h1>New Auto</h1>
<%= render 'form' %>
<%= link_to 'Back', autos_path %>
代码执行工作......
问候