Rails 3.2 create a form that's used in the footer of every page

前端 未结 2 646
时光说笑
时光说笑 2021-01-17 03:44

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

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 03:55

    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('
      '); <%= @mailing_list.errors.full_messages.each do |msg| %> $('.error-messages').append('
    • <%= escape_javascript msg %>
    • '); <% end %> <% end %>

    提交回复
    热议问题