Question

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

Was it helpful?

Solution 2

Try

<%= form_for SomeThing.new do |f| %>

OTHER TIPS

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.

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 %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top