0

好吧,我对 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 仍然拒绝执行。

为什么?

编辑//相关路线:

  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
4

0 回答 0