6

I have a location model and a post model. I want to allow the user to create new posts from the index page of the location model. How do I do that? I tried rendering the post new.html.erb as a partial in location index.html.erb, but this leads to errors as post new.html.erb references the @post variable.

How should I accomplish this?

ankit
  • 3,328
  • 3
  • 26
  • 39

3 Answers3

16

Try something like this

LocationController

def index
  @location = Location.find(:all)
  @post = Post.new
end

Now ur location view would have access to the instance variable @post. Render a partial using that

render :partial => 'posts/post', :object => @post
Rahul
  • 44,892
  • 25
  • 73
  • 103
  • +1, ..or, in a way similar to what Allesklar proposes: use `Post.new` instead of the variable, but this depends on the view: the variable may be 'hardcoded' there. – Arsen7 Aug 25 '11 at 12:08
3

You can start the form like this:

<%= form_for Post.new do ... %>
allesklar
  • 9,506
  • 6
  • 36
  • 53
0

Just create a @post variable in your action in the LocationsController.

Arsen7
  • 12,522
  • 2
  • 43
  • 60
  • That gives me this error: `Expected /home/eyekandy/rails_apps/eyekandy/app/controllers/locations_controller.rb to define LocationsController` – ankit Aug 25 '11 at 10:54
  • Then you have a **syntax error** in your code. Could you show the code of your action? I assume that you edited only the action, but the error may be somewhere else - maybe you accidentally removed some character? – Arsen7 Aug 25 '11 at 11:31