0

我有一个 rails 应用程序,我想通过模态弹出窗口做一个评论功能。如何Comment在按下按钮时调用模型的新方法,以便@comment在绘制表单时不为零。这是我的代码:

<div class='well well-backdrop'>
 <a href="#commentModal" role="button" class="btn btn-mini btn-custom-primary" data-toggle="modal">Post Comment</a>
</div>

<div id="commentModal" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body">
  <%= form_for(@comment) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

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

  <div class="field">
    <%= f.label :user_id %><br />
    <%= f.number_field :user_id %>
  </div>
  <div class="field">
    <%= f.label :comment %><br />
    <%= f.text_field :comment %>
  </div>
  <div class="field">
    <%= f.hidden_field :illustrationId, :value => @illustration.id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn btn-mini">Cancel</a>
    <a href="#" class="btn btn-mini btn-custom-primary">Submit</a>
  </div>
</div>

我已经将我的代码重新格式化为这个并且提交不起作用:

<div class='well well-backdrop'>
 <a href="#commentModal" role="button" class="btn btn-mini btn-custom-primary" data-toggle="modal">Post Comment</a>
</div>

<div id="commentModal" class="modal hide fade">
  <div class="modal-body">
    <%= form_for(Comment.new, remote: true, html: {"data-type" => :json}, :validate => true) do |f| %>
    <%= f.hidden_field(:illustrationId, :value => @illustration.id) %>  
    <%= f.text_area(:comment, :id => "comment_message") %>
    <%= f.submit "Submit", :class => 'btn btn-custom-primary' %>
    <% end %>

  </div>
</div>

这是我的 js 日志中发生的事情,在我手动执行之前,加载 ../comments 似乎存在问题。然后我得到一个不同的错误(第 3 和第 4 行):

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://general-rails-13774.use1.actionbox.io:3000/comments
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://general-rails-13774.use1.actionbox.io:3000/comments
POST http://general-rails-13774.use1.actionbox.io:3000/comments 500 (Internal Server Error) jquery.js:8527
POST http://general-rails-13774.use1.actionbox.io:3000/comments 500 (Internal Server Error) application.js:22
4

1 回答 1

1

我在模态中得到了这个,它工作得很好:

<%= form_for(Order.new, remote: true, html: {"data-type" => :json}, :validate => true) do |f| %>
  <%= f.check_box(:user_accepts, :class => 'acceptance') %>
  <%= f.hidden_field(:quantity, :value => p.quantity) %>
  <%= f.hidden_field(:price, :value => p.price) %>
  <%= f.submit "Confirm", :class => 'btn btn-primary orderconfirm' %>
<% end %>

有了这个咖啡脚本:

jQuery ->
  $("form.new_order").on "ajax:success", (event, data, status, xhr) ->
    $('.modal').modal('hide')
    window.location.replace("/");
于 2013-07-09T23:51:07.900 回答