1

I want to place my <%= form_for(@something) do |f| %> which is currently located in app/views/something/new.html -- inside multiple pages, so maybe in app/views/layouts/application.html.erb

How do I get the @something variable and the form to work properly there, or somewhere else -- since it's defined in the controller #new action of SomethingController, it only seems to be available in the appropriate new.html.erb view..

3 Answers3

2

You can put the form anywhere, just provide an instance variable of @something in controller

The basic usage is here.

ThisThingsController
  def show
    @this_thing = foo
    @that_thing  = bar
  end
end

# View
<%= @this_thing %>
<%= form_for @that_thing %>

Of course you can use partial to render the form, as long as you feed it with variable it needs.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
1

Without fully understanding what you are trying to accomplish, I'll make this suggestion. Add a before filter to your ApplicationController (alternatively you could create a module and mix it in where needed). Then call the before_filter when needed. This example will always run the before filter:

class ApplicationController
  before_filter :set_something

  private
  def set_something
    @something = ...  # Fill in the logic here
  end
end

Then add your form where needed. You can even make it appear conditionally depending on whether @something is set.

<% if @something %>
  # Form goes here
<% end %>
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43
1

Try

<%= form_for SomeThing.new do |f| %>
David
  • 7,310
  • 6
  • 41
  • 63