在Rails中使用回形针的远程表格?

时间:2012-06-20 17:39:39

标签: ajax ruby-on-rails-3 forms

我有一个Rails应用程序,并尝试制作一个远程表单,到目前为止它还没有真正解决..有人能指出我在这里做错了什么:

所以模特:

class Media < ActiveRecord::Base
  attr_accessible :galaxy, :title, :rss_url, :avatar
  has_many :stories
  has_many :plots, :through => :stories




class Story < ActiveRecord::Base
  attr_accessible :content, :galaxy, :title
  validates_uniqueness_of :content
  belongs_to :media
  has_many :plots




class Plot < ActiveRecord::Base
  belongs_to :plotable, :polymorphic => true
  has_many :plots, :as => :plotable
  has_many :soundtracks, :as => :soundtrackable, :dependent => :destroy
  accepts_nested_attributes_for :soundtracks, :allow_destroy => true

所以在app / views / medias / show.html.erb ::

中查看
  <% @media.stories.each do |s| %>
   <%= form_for :plot, :remote => true, :html => { :id => 'pl_form' } do |f| %>
    <%= f.fields_for :soundtracks do |soundtrack_fields| %> 
       <%= soundtrack_fields.file_field :soundtrack %>
     <% end %>
    <%= text_field_tag :name, s.content, :style =>"display:none" %>
    <%= f.submit %>
   <% end %> 

应用程序/视图/曲线/ create.js.erb

var el = $('#new_pl');
  // We could also render a partial to display the plot here
  $('#plots').append("<%= escape_javascript( 
      simple_format( @plot.body ) 
    ) %>");
  el.find('input:text,textarea').val('');
  el.find('.errors').empty();
<% end %>

控制器是标准的..

1 个答案:

答案 0 :(得分:0)

将multipart true添加到表单

 <%= form_for :plot, :remote => true, :html => { :id => 'pl_form', :multipart => true } do |f| %>
来自W3C的

Multipart

  

内容类型“multipart / form-data”应该用于提交包含文件,非ASCII数据和二进制数据的表单