【问题标题】:How to convert a working rails contact form, into a bootstrap modal?如何将工作轨道联系形式转换为引导模式?
【发布时间】:2013-03-06 08:01:19
【问题描述】:

我目前有一个可用的 Rails 联系表(取自另一个 SO 答案),可在 /contact 上找到,它使用控制器、模型和邮件程序 (Mandrill)。它在控制器中响应 new 和 create 路由,并且仍然使用 activemodel 功能。表单不使用 ajax。

我现在需要尝试让我的联系表单在主页上工作,而不是通过模式弹出窗口在 /contact 页面上工作。

我已经阅读了一些关于模态的 SO 查询,但似乎都与让模态做特定的事情有关 - 而不是创建模态表单来模仿正常表单。

首先,我在主页上添加了一个工作模式。

当我尝试将表单添加到主页模型中时,我遇到了方法错误,因为表单是 home_controller 的一部分。在将我的新和创建控制器操作复制到我的主控制器后,我意识到表单只是隐藏了,并且在页面加载时仍在运行(通过索引操作)。 将@message = Message.new 添加到索引操作中似乎没有帮助 - 我不希望在模态负载上这样做吗?

这是我的联系页面中工作表单的主要移动部分,以及主页中的工作模式框 - 我如何合并两者并保留我拥有的功能?

这里是联系人控制器:

#/app/controllers/contact_controller.rb
class ContactController < ApplicationController

 def new
  @message = Message.new
 end

 def create
  @message = Message.new(params[:message])

  if @message.valid?
     NotificationsMailer.new_message(@message).deliver
     redirect_to(root_path, :notice => "Message was successfully sent.")
  else
   flash.now.alert = "Please fill all fields."
   render :new
  end

 end
end

消息模型

#app/models/message.rb
class Message

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :subject, :body

  validates :name, :email, :subject, :body, :presence => true
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

还有观点:

 <%= form_for @message, :url => contact_path do |form| %>
  <fieldset class="fields">
    <div class="field">
      <%= form.label :name %>
      <%= form.text_field :name %>
    </div>

    <div class="field">
      <%= form.label :email %>
      <%= form.text_field :email %>
    </div>
    <div class="field">
      <%= form.label :subject %>
      <%= form.text_field :subject %>
    </div>

    <div class="field">
      <%= form.label :body %>
      <%= form.text_area :body %>
    </div>
  </fieldset>

  <fieldset class="actions">
   <%= form.submit "Send" %>
  </fieldset>
<% end %>

最后在我的主页中,我有这个模式(不包含表单)

<div class="modal fade" id="modal_contact_form">

  <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
      <h3>Contact Form</h3>
  </div>
  <div class="modal-body">
    <p>Test Content for Modal</p>
  </div>
  <div class="modal-footer">
 </div>
</div>

这是&lt;li&gt;&lt;a href="#modal_contact_form" data-toggle="modal"&gt;contact&lt;/a&gt;&lt;/li&gt;调用的

直到这一点,我的尝试已经很多并且不同了 - 我需要解决的主要问题(我认为)是表单是由 home_controller 上的索引操作加载的 - 理想情况下我猜我还想使用我的contact_controller 中的逻辑吗?

任何建议和建议都值得赞赏 - 在这一点上,我想知道是否有一种简单的方法可以将表单移动到模式中!

干杯,

米斯巴

注意:在回答我对 ajax 的疑问时,我只是避免使用 ajax,因为在 Rails 3.2 上尝试它时感觉困难和缺乏进展 - 如果它实际上更容易打开它,我会很高兴使用它变成一个ajaxified形式?

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap controller modal-dialog contact-form


    【解决方案1】:

    看来我毕竟不远了!

    在#rubyonrails 频道的指针之后,答案是:

    1) 将表单原样复制到模态中。 (请注意,可能需要不同的样式 - 但这个答案提供了所需的功能)。

    2) 将@message = Message.new 添加到我的 home_controller.rb 的索引操作中

    我曾尝试过与此非常相似的方法,但肯定会引入另一个错误,因此绕过了正确的解决方案。

    【讨论】:

    • 他们的路线怎么样?请在这里发帖好吗?
    猜你喜欢
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多