2014-02-25 16 views
0

我有兩個型號,:組和:投我試圖通過AJAX到一個鏡頭添加到組。以下是相關的文件...Rails的Ajax表單顯示

#app/views/groups/show.html.erb 
    <%= link_to 'New Shot', new_shot_path(group_id: @group), id: "new_shot", remote: true %></br> 

    #app/views/shots/new.js.erb 
    $('#new_shot').hide().after('<%= j render("form") %>'); 

#app/views/shots/new.html.erb 
<h1>New shot</h1> 
<%= render 'form' %> 
<%= link_to 'Back', shots_path %> 

#app/views/shots/_form.html.erb 
<%= form_for(@shot) do |f| %> 
    <% if @shot.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@shot.errors.count, "error") %> prohibited this shot from being saved:</h2> 

     <ul> 
     <% @shot.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <%= f.hidden_field :group_id, :value => params[:group_id] %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label 'Comedian' %><br /> 
    <%= 
     @comedians = Comedian.all 
     collection_select(:shot, :comedian_id, @comedians, :id, :name) 
    %> 
    </div> 
    <%= f.file_field :pic %> 
    <%= image_tag @shot.pic.url(:thumb) %> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

一切看起來都在控制檯良好...

Started GET "/shots/new?group_id=1" for 127.0.0.1 at 2014-02-25 10:09:19 -0500 
Processing by ShotsController#new as JS 
    Parameters: {"group_id"=>"1"} 
    Comedian Load (0.1ms) SELECT "comedians".* FROM "comedians" 
    Rendered shots/_form.html.erb (12.1ms) 
    Rendered shots/new.html.erb within layouts/application (12.7ms) 
Completed 200 OK in 93ms (Views: 52.4ms | ActiveRecord: 0.6ms) 

它只是沒有呈現在頁面上。任何想法我失蹤?我知道回形針(我正在使用)有一些問題,但我試圖從頁面中刪除這些引用,並得到相同的結果。另外,我正在使用twitter bootstrap。不知道這是否會導致問題。

+0

你最有可能缺少ID爲「new_shot」您的頁面上的div元素 –

+0

您所提出的建議,這應該是在應用程序/視圖/組/ show.html.erb? – Lumbee

+0

你使用的是什麼版本的Rails?你還可以包含'ShotsController'。 –

回答

0

所以......

我的修復是修改shots_controller刪除響應以阻止從這個網站和JSON ...

def new 
    @shot = Shot.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @shot } 
    end 
    end 

這個...

def new 
    @shot = Shot.new 
    end 

有人可以解釋爲什麼這需要刪除的Ajax工作?