rails 3 and jquery railscast 197 issues



我在做railscast 196-197集,除了没有"添加答案"、"添加问题"one_answers"删除问题"之外,一切都很好,我在处理ruby1.9.2和rails 3.1.1,我知道这一集在rails 2.3中,但一定是我错过的a=或%…在这一集结束时,给jquery一个选项,但对我不起作用,请帮助!提前谢谢。

感谢死亡之锤。这为我解决了同样的问题。

这是Rails3中helper方法的语法变化造成的

Railscast ex:

link_to_function(name, h("add_fields(this, "#{association}", "#escape_javascript(fields)}")"))

您的工作前任:

link_to_function(name, ("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))

由于您没有提供任何细节,所以我是根据自己的假设编写的。将此内容写入您的应用程序_help.rb

def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end
  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, ("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
  end

在你的应用程序.js 中写下这个

function remove_fields (link) {
  $(link).prev("input[type=hidden]:first").val('1');
    $(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).parent().before(content.replace(regexp, new_id));

}

这在某种程度上是你的观点:

<%= f.fields_for :uploads do |upload| %>
    <div class="fields">
      Upload Photo: <%=upload.file_field :photo %>
    </div>
<% end %>
<p> <%= link_to_add_fields 'Need to upload More files? Please click here.', f, :uploads %> </p>

创建一个带有_modelname_fields.html.erb的分部,它应该只有这个字段(对我来说是一个文件上传字段),比如这个

<div class="fields">
  Upload Photo: <%=f.file_field :photo %>
</div>
<br />

试试看。

最新更新