简体   繁体   English

将AJAX实施为rails形式。

[英]Implementing AJAX into a rails form.

I have a from in which a user can send out a message via SMS. 我有一个用户可以从中通过SMS发送消息的消息。 The form functions with the capability of scheduling out a message. 该表单具有调度消息的功能。 Right now I have a static button that says "send message" and it works but when a user schedules a message I'd like the button to switch the inside text to read "schedule message" by an onlcick type action. 现在,我有一个静态按钮,上面写着“发送消息”,它可以工作,但是当用户安排一条消息时,我希望按钮可以通过onlcick类型的操作将内部文本切换为“计划消息”。 I think this should be an esay thing to implement using ajax but I've never used it before in a rails app. 我认为这应该是使用ajax实现的易事,但我以前从未在Rails应用程序中使用过它。 I'll show a screen shot and some code, hopefully this will be enough info. 我将显示一个屏幕截图和一些代码,希望这将是足够的信息。 let me know if you need anymore provided code than I initially show. 让我知道您是否需要提供比我最初显示的更多的代码。

Here is my form screenshot, so when a user clicks on the calender glyphicon it will initiate the ajax and switch the "Send Message" button to say "Schedule Message" 这是我的表单屏幕截图,因此,当用户单击日历压延字形图标时,它将启动ajax并切换“发送消息”按钮以显示“计划消息” 在此处输入图片说明

Here is the code for my form 这是我的表格的代码

<div class="container text-center">
 <div class="row">
  <div class="col-lg-8 col-lg-offset-2 well">
     <%= form_for @message do |form| %>
       <div class="form-group">
         <%= form.label :body, "Enter a Message:", class: "message_label"%>
         <%= form.text_field :body, class: "form-control", placeholder: "New things are happening!" %>
       </div>
       <%= form.submit "Send Message", class: "btn btn-primary" %>
       <h3>Groups:</h3>
       <div class="radio">
         <div class="row col-span10">
           <% Group.all.each do |group| %>
             <div class="col-md-3">
               <label>
                 <input id="message_group_<%= group.id %>" name="message[group_ids][]" value="<%= group.id %>" type="checkbox">
                 <%= group.name %>
               </label>
            </div>
          <% end %>
        </div>
      </div>
      <h3>Scheduled Text Message:</h3>
      <div class="row">
        <div class='col-sm-4 col-sm-offset-4'>
          <div class="form-group">
            <div class='input-group date' id='datetimepicker'>
              <%= form.text_field :send_at, class: "form-control" %>
              <span class="input-group-addon">
                  <span class="glyphicon glyphicon-calendar"></span>
              </span>
            </div>
          </div>
        </div>
      </div>
    <% end %>
  </div>

I suggest you to read Working with javascript in Rails , there it explain several things about javascript and AJAX. 我建议您阅读在Rails中使用javascript ,那里介绍了有关javascript和AJAX的几件事。

Basically, you need to tag your form as remote and hook with javascript to AJAX events (success, complete, error, etc) to perform any animation/transition you want. 基本上,您需要将表单标记为remote并使用javascript挂钩到AJAX事件(成功,完成,错误等),以执行所需的任何动画/过渡。

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

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