2011-09-06 14 views
0

我正在通過一個簡單的配方跟蹤程序來學習Rails。這裏是我卡住的地方:當我在表單上創建系統中的新配方時,如果該配方的來源(即書籍,雜誌等)目前不存在,我也想創建它時間。換句話說,當我在看'recipe/new.html'時,如果我需要添加一個新的源代碼,使用jQuery對話框(或者更有意義的另一種方法),我想渲染'source/_form.html。 erb'(新的配方源的部分),創建一個新的配方源,然後在我的選擇框的末尾註入新的記錄。Rails模式對話框創建記錄,然後關閉更新頁面

從食譜/ new.html:

<div class="field"> 
    <%= f.label :source %><br /> 
    <%= select("recipe", "source_id", Source.all.sort_by(&:title).collect {|s| [ s.title, s.id ] }, { :include_blank => true }) %> 
    <%= link_to "new source", new_source_path (@source, :format => :js), :remote => true %> 
</div> 

從源/ new.js.erb

$("<%= escape_javascript render(:partial => 'sources/form') %>").dialog(); 
$('#new_comment_link').hide(); 

從控制器/ sources_controller.rb

# GET /sources/new 
    # GET /sources/new.xml 
    def new 
    @source = Source.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @source } 
     format.js 
    end 
    end 

    # GET /sources/1/edit 
    def edit 
    @source = Source.find(params[:id]) 
    end 

    # POST /sources 
    # POST /sources.xml 
    def create 
    @source = Source.new(params[:source]) 

    respond_to do |format| 
     if @source.save 
     format.html { redirect_to(@source, :notice => 'Source was successfully created.') } 
     format.xml { render :xml => @source, :status => :created, :location => @source } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @source.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

來源/形式部分,這應該呈現在jQuery對話框然後消失提交。當對話關閉我想通過這種形式附加到我的食譜/ new.html選擇框年底創建的新紀錄:

<%= form_for(@source) do |f| %> 
    <% if @source.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@source.errors.count, "error") %> prohibited this source from being saved:</h2> 

     <ul> 
     <% @source.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.label :author_last %><br /> 
    <%= f.text_field :author_last %> 
    </div> 
    <div class="field"> 
    <%= f.label :author_first %><br /> 
    <%= f.text_field :author_first %> 
    </div> 
    <div class="field"> 
    <%= f.label :media_type %><br /> 
    <%= f.text_field :media_type %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

感謝您抽出通過這個閱讀時間。

編輯:爲了更明確地說明我的問題是什麼,上面的代碼呈現一個對話框並允許我提交一個新的源代碼。但是,在提交之後,它會重定向到新來源的視圖頁面,而不是在我打開對話框之前開始的新配方表單。

+0

你有關於這個問題的消息嗎?我有完全相同的問題,我一直無法解決它。 – brunomac

回答

0

所以,畢竟,我只需要改變我的偏見。這種變化是從:

<%= form_for(@source) do |f| %> 

<%= form_for (@source, :remote => true) do |f| %> 

所以,至少我現在提交記錄到數據庫中。不幸的是,現在我無法在提交後關閉對話框。我會做更多的研究,並在必要時提出另一個問題。

相關問題