Rails 3.2 Create a Form That's Used in the Footer of Every Page

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

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 %>

creating a form that is present in header of site rails

The form_for helper expects an url, which you can specify manually if the path rails assumes by default is incorrect:

<%= form_for MailingList.new, url: '/sign_up', html: { remote: true } do |f| %>
<%= f.text_field :email %>
<% end %>

If you really want a form without an url, just write out the tag manually

<form>
<%= text_field_tag :email %>
</form>

Rails 3.2 - Create and Update button using GET instead of POST & PUT

Check out How do forms with PATCH, PUT, or DELETE methods work? Basically it is a GET with a hidden "_method" field to get around the limitation of some browsers not implementing PUT and DELETE.

This is handled when you use form_for. What does your form_for line and routes look like?

Adding extra layout to specific controllers and actions in rails 3?

Replace your render with this:

 <%= render 'layouts/dropdown' unless @disable_dropdown %>

Then you can simply set disable_dropdown to true in any controller you like:

def test_method
@disable_dropdown = true
end

call this method in your controller filter, in which you dont want to show this:

write this on top of your controller above your first method:

before_filter :test_method

it will automatically be called when your request comes to this controller.

Hope it will help. Thanks

how to use content_for so that the content shows up in the layout

OK, I think I have a solution for this. Your code:

<% content_for :navigation do %>
<ul>
<li>Home</li>
</ul>
<% end %>

should be at the top of the file that is loading. Your _header.html.erb is a partial. If you move this code into views/tasks/new.html.erb then it works as expected.

However, for it to work as you want, then you need to adjust your application.html.erb file:

<p>this is where we should see the "Home" link appear that is defined in _header.html.erb:</p>
<section class="header">
<% render 'layouts/header' %>
<%= yield :navigation %>
</section>

Notice, that I have called the render erb tag without the = sign. This means I don't see the contents of the header partial, but it does load. If you include the = sign then it still works but also renders any other content you may have in the partial.
Note: The render tag has to be above/before the yield tag.



Related Topics



Leave a reply



Submit