开发者

Rails modal dialog that creates record then updates page on close

I am learning Rails by making a simple recipe tracking program. Here's where I'm stuck: when I am on the form to create a new recipe in the system, if that recipe's source (ie a book, magazine, etc) doesn't currently exist I would like to create that too at the same time. In other words when I am looking at 'recipe/new.html', if I need to add a new source, using jQuery dialog (or another approach if that makes more sense) I would like to render 'source/_form.html.erb' (the partial for a new recipe source), create a new recipe source, and then inject that new record at the end of my select box.

From recipe/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>

From sources/new.js.erb

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

From controllers/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

The sources/form partial, which should render in jquery dialog then disappear on submit. When dialogue closes I want the new record created by this form to appended to the end of my select box on recipe/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 %>

Thank you for taking the time to read through this.

EDIT: To be more explicit about what my problem is, the code above renders a dialog and allows me to submit a new source. However, after submission it redirects to the new source's view page, rather than the new recipe form I started on before I opened the dialog.


So, after all that, I just needed to change one thing in my partial. The change was from:

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

to

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

So at least I am now submitting records to the database. Unfortunately, now I can't get the dialog to close after submit. I'll do more research about that and ask another question if necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜