开发者

Rails form multiple views?

I'm looking for a way to have a contact form in the application layout and show it on all pages.开发者_运维技巧

Ideally i'd like to just do a

form_for @contact_us

I've tried creating the instance variable in the application controller but it doesnt seem to be in scope when the layout loads.. (does the layout load before the result of the action??)

I guess id settle for a form_tag.

Whats the ususal way of doing this kinda thing?

Thanks!


You can use a partial. Put the form code (use form_tag) in the partial and render the partial in the layout.

More about partials here.


What data are you assigning to @contact_us? You might consider using form_tag rather than form_for if your form doesn't require a resource.

Set up whatever you need in your application controller...

before_filter :prepare_contact_form

def prepare_contact_form
  @contact_us = "The stuff your form needs"
end

Create a partial view containing your form. Assuming form_tag meets your needs, for lack of more information...

<!-- app/views/_contact_form.html.erb -->
<%= form_tag "/contact_us" do %>
  <%= @contact_us %>
  <%= text_field_tag :from_email %>
  <%= text_field_tag :message %>
  <%= submit_tag 'Submit' %>
<% end %>

Render the partial in your application layout...

<!-- app/views/layouts/application.html.erb -->
render :partial => "contact_form"

Then handle the request in whichever controller action /contact_us is routed to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜