Rails表单提交给控制器动作和jQuery模式

时间:2013-10-08 19:34:08

标签: jquery ruby-on-rails forms ruby-on-rails-4 modal-dialog

我希望表单提交(POST)我的控制器中的自定义操作,它将执行评估(基于表单中的数据),然后弹出一个具有该评估结果的jQuery模型。诀窍是,我希望这个模态只是弹出表单,这样如果他们关闭模态,他们可以返回并查看/编辑表单。

我尝试了类似下面的内容,但它不起作用。我收到以下错误:

缺少模板游戏/评估,应用/评估{:locale => [:en],:formats => [:html],:handlers => [:erb,:builder,: raw,:ruby,:coffee,:haml]}。

/app/controllers/games_controller.rb

class ExercisesController < ApplicationController
  #...    
  def evaluate
    if( params[:id] )
      found = Game.find(params[:id])
      @eval = found.evaluate_guess(params[:guess])
    end  
  end
  #...
end

/app/views/games/play.html.haml

= semantic_form_for @games, :url => evaluate_path(@games) do |f|
  = f.semantic_errors
  = f.input :guess, 
    :as => :text
= f.actions do
  = f.action :submit, :label => "Guess!",
    :button_html => { :action => :evaluate, :method => :post } 

/app/views/games/_evaluation_modal.html.haml

#evaluation_modal.modal.modal-wide.hide.fade
  = semantic_form_for @exercise, html: { class: 'modal-form' } do |f|
    .modal-header
    %h3 Results

    .modal-body
      #flashbar-placeholder
      %h2= CGI.unescapeHTML(@eval).html_safe

/app/views/games/evaluate.js.coffee

$('#evaluate').html(
  '<%= escape_javascript render partial: "evaluation_modal"  %>')
$('#evaluation_modal').modal('show')

/config/routes.rb

#...
get '/play/:id' => 'games#play', as: :play
patch '/play/:id' => 'games#evaluate', as: :evaluate
#...

1 个答案:

答案 0 :(得分:0)

尝试将:remote => true添加到您的play表单。

= semantic_form_for @games, :url => evaluate_path(@games), :remote => true do |f|
相关问题