如何在显示引导模式的按钮上调用控制器方法?

时间:2013-07-09 23:42:45

标签: ruby-on-rails twitter-bootstrap

我有一个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

1 个答案:

答案 0 :(得分: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 %>

用这个coffeescript:

jQuery ->
  $("form.new_order").on "ajax:success", (event, data, status, xhr) ->
    $('.modal').modal('hide')
    window.location.replace("/");