繁体   English   中英

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

[英]Rails form submit to controller action and jQuery modal

我希望表单提交(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
#...

尝试在play表单中添加:remote => true

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM