Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Show form from different model in a view

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?

like image 871
Ankit Soni Avatar asked Aug 25 '11 10:08

Ankit Soni


2 Answers

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
like image 180
Rahul Avatar answered Oct 14 '22 02:10

Rahul


You can start the form like this:

<%= form_for Post.new do ... %>
like image 32
allesklar Avatar answered Oct 14 '22 02:10

allesklar