さて、Rails に小さな問題があり、頭がおかしくなりました。
私はsimple_formを持っています:
<section id="address_form">
<%= simple_form_for [:cockpit, @location, @address], :remote => true, :html => {:id => "location_address_#{@location.id}"} do |f| %>
<%= f.input :latitude, :as => :hidden %>
<%= f.input :longitude, :as => :hidden %>
<%= f.input :gmaps, :as => :hidden %>
<%= f.input :address_line_1, label: false, placeholder: "Address Line 1" %>
<%= f.input :address_line_2, label: false, placeholder: "Address Line 2" %>
<%= f.input :city, label: false, placeholder: "City" %>
<%= f.input :state, label: false, placeholder: "State" %>
<%= f.input :zip, label: false, placeholder: "Zip" %>
<div class="row">
<div class="span4">
<%= f.input :country, :collection => Carmen::Country.all.map(&:name), :selected => resource.country, :include_blank => true, prompt: :Country, label: false %>
</div>
<div class="span3">
<%= link_to "Save", "#", :class=> "btn", :"data-blocker" => true, :"data-blocker-element" => "location_address_#{@location.id}", :"data-sendable" => true, "data-sendable-form" => "location_address_#{@location.id}" %>
</div>
</div>
<% end %>
</section>
現在、フォームを送信するたびに 406 Not Acceptable を取得し続け、確認するまで、その理由について少しの手がかりがありませんでした (406 は無効なリクエスト タイプにのみ使用されます。たとえば、コントローラーが js リクエストにのみ応答する場合など)。 Chrome の開発ツールでのリクエスト:
何らかの理由で、Rails は URL にドットと ID を追加し、無効なリクエスト形式でリクエストを処理します。
私は先に進み、そのアクションのリクエスト形式を手動で js に設定しました。
class Cockpit::AddressesController < Cockpit::BaseController
helper_method :location
before_filter :set_format, only: [:update]
inherit_resources
respond_to :js, :html
def update
update! do |format|
@json = location.address.reload.to_gmaps4rails
format.js
end
end
def set_format
request.format = "js"
end
protected
def location
@location ||= Location.find(params[:location_id])
end
end
現在、リクエストは正常に処理され、.js.erb ファイルはレンダリングされていますが、その中の Javascript コードは実行されていません。これは応答です:
LtNoty.success("Changes saved!");
window.location.href = "/cockpit/locations/22/address";
これは .js.erb ファイルです。
<% if resource.errors.any? %>
LtNoty.error("Processing your request failed!");
<% else %>
LtNoty.success("Changes saved!");
window.location.href = "/cockpit/locations/<%= params[:location_id] %>/address";
<% end %>
<% puts 'test' %> を .js.erb ファイルに入れると、コンソールに出力が表示されますが、Javascript は依然として実行を拒否します。
なんで?
EDIT // 関連するルート:
namespace :cockpit do
resources :locations do
resources :tabs
resources :pictures
resources :location_activities
resources :location_prices
resource :address
resources :reviews
end
resources :supports
resources :pictures
resources :activities
resources :prices
resources :requests do
resources :responses
match 'new/:state' => 'responses#new', :as => :new_response_state
end
resources :reservations
root :to => "home#index"
end