Ruby on rails 通过ajax呈现编辑表单

Ruby on rails 通过ajax呈现编辑表单,ruby-on-rails,ajax,forms,simple-form,Ruby On Rails,Ajax,Forms,Simple Form,我正在尝试通过ajax呈现表单。在创建新场馆时,它可以正常工作,但在尝试编辑现有场馆时,应将场馆数据加载到表单中。调用“编辑地点”链接时,将呈现表单(_model),但表单将作为新表单处理,所有字段均为空。在binding.pry处调用@venue,我们可以看到正确的venue对象 如何使表单以编辑而不是新的方式处理?想法: 在表单中添加更多参数 手动执行-使用jquery更改表单属性和/或填写数据 感谢您的建议 _modal.html.erb: <section id="venue

我正在尝试通过ajax呈现表单。在创建新场馆时,它可以正常工作,但在尝试编辑现有场馆时,应将场馆数据加载到表单中。调用“编辑地点”链接时,将呈现表单(_model),但表单将作为新表单处理,所有字段均为空。在binding.pry处调用@venue,我们可以看到正确的venue对象

如何使表单以编辑而不是新的方式处理?想法:

  • 在表单中添加更多参数

  • 手动执行-使用jquery更改表单属性和/或填写数据
感谢您的建议

_modal.html.erb:

<section id="venue-modal" class='modal' title="Venue Form" class="">
  <%= simple_form_for(@venue, remote: true) do |v| %>
    <div id='closeButton'><a href='#'>x</a></div>
  <% binding.pry %>
    <%= v.input :name, label: "Venue Name" %>
    <%= v.input :city %>
    <%= v.input :state, collection: States.us_states %>

    <div class="actions">
      <%= v.submit 'Submit', class: 'button' %>
    </div>
  <% end %>
</section>
vitune.js:

$('.edit_venue_link').on("ajax:success", function(e, data, status, xhr) {
// stuff happens

用modal.js.erb中的这行代码替换模态内容:

$('#venue-modal').html("<%= escape_javascript(render partial: 'venues/modal') %>");
节标记已移出模式部分。然后表单会识别@venture,并相应地加载字段

<%= link_to edit_venue_path(venue), :remote => true, :class => "edit_venue_link" do %>
def edit
  id = params[:id]
  if id == 'new'
    @venue = Venue.new
  else
    @venue = Venue.find(id)
  end
end
$('.edit_venue_link').on("ajax:success", function(e, data, status, xhr) {
// stuff happens
$('#venue-modal').html("<%= escape_javascript(render partial: 'venues/modal') %>");
def edit
  id = params[:id]

  if id == 'new'
    @venue = Venue.new
  else
    @venue = Venue.find(id)
  end
  respond_to do |format|
    format.js { render 'venues/modal' }
  end
end