Rails 3.2 创建一个用于每个页面页脚的表单 [英] Rails 3.2 create a form that's used in the footer of every page

查看:22
本文介绍了Rails 3.2 创建一个用于每个页面页脚的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表单,用于注册我网页页脚中的邮件列表.我所做的是创建一个部分,在应用程序布局的页脚中呈现这个小表单.

这是部分代码:

<%= form_for(@mailing_list) do |f|%><% 如果@mailing_list.errors.any?%><div id="error_explanation"><h2><%=pluralize(@mailing_list.errors.count, "error") %>禁止保存此邮件列表:</h2><ul><% @mailing_list.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="field"><%= f.label :first_name %><br/><%= f.text_field :first_name %>

<div class="field"><%= f.label :last_name %><br/><%= f.text_field :last_name %>

<div class="field"><%= f.label :email %><br/><%= f.text_field :email %>

<div class="actions"><%= f.submit %>

<%结束%>

我仍在学习 Rails,所以它是从脚手架生成的代码.从我收集的信息来看,我需要使用 @mailing_list = MailingList.new 实例化 @mailing_list 变量,但这里的问题是邮件列表控制器中的 NEW 操作没有被调用,因为我不一定要访问该页面.此表单位于每个页面的页脚中.

创建此表单的正确方法是什么?有没有办法在不调用每个控制器中的 MailingList.new 的情况下做到这一点?

谢谢!

解决方案

把你的表格改成这个

<%= form_for MailingList.new, html: { remote: true } do |f|%>

所以你不必担心实例变量.您还应该传递 remote: true 以便通过 ajax 提交表单.要显示错误,请在 app/views/mailing_lists 下创建一个名为 create.js.erb 的文件并添加以下内容(只是一个简单的脚本,将错误附加到表单之前)

$('.error-messages').remove();<% 如果@mailist_list.errors.any?%>$('#new_mailing_list').before('<ul class="error-messages"></ul>');<%= @mailing_list.errors.full_messages.each do |msg|%>$('.error-messages').append('<li><%=escape_javascript msg %></li>');<%结束%><%结束%>

I want to create a form that's used to sign up to a mailing list in the footer of my webpage. What I did was create a partial that renders this small form in the footer of the application layout.

Here is the code for the partial:

<%= form_for(@mailing_list) do |f| %>
  <% if @mailing_list.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@mailing_list.errors.count, "error") %> prohibited this mailing_list from being saved:</h2>

      <ul>
      <% @mailing_list.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I'm still learning rails so that's generated code from scaffolding. From what I gather I need to instantiate the @mailing_list variable with @mailing_list = MailingList.new but the problem here is that NEW action in the mailing list controller doesn't get called because I'm not necessarily visiting that page. This form is in the footer of every page.

What would be the proper way to create this form? Is there a way to do this without calling MailingList.new in every controller?

Thanks!

解决方案

change your form to this

<%= form_for MailingList.new, html: { remote: true } do |f| %>

so you don't have to worry about instance variables. You should also pass remote: true so the form is submitted via ajax. To show error, create a file called create.js.erb under app/views/mailing_lists and add the following (just a simple script that appends the errors before the form)

$('.error-messages').remove();
<% if @mailist_list.errors.any? %>
  $('#new_mailing_list').before('<ul class="error-messages"></ul>');
  <%= @mailing_list.errors.full_messages.each do |msg| %>
    $('.error-messages').append('<li><%= escape_javascript msg %></li>');  
  <% end %>
<% end %>

这篇关于Rails 3.2 创建一个用于每个页面页脚的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆