Rails不执行javascript响应

时间:2013-07-30 11:29:05

标签: javascript ruby-on-rails ajax simple-form

好吧,我对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不可接受的内容,而且我没有丝毫关于原因的线索(406仅用于无效的请求类型,例如,如果您的控制器只响应js请求)直到我在Chrome的开发工具中检查了该请求:

response

出于某种原因,Rails会在网址上附加一个点和一个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 %>

如果我把&lt;%puts'测试'%&gt;在.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

0 个答案:

没有答案