【问题标题】:Rails form submit to controller action and jQuery modalRails 表单提交到控制器动作和 jQuery 模式
【发布时间】:2013-10-08 19:34:08
【问题描述】:

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

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

缺少模板游戏/评估,使用 {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :红宝石,:咖啡,: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
#...

【问题讨论】:

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


    【解决方案1】:

    尝试将:remote =&gt; true 添加到您的play 表单中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      相关资源
      最近更新 更多