Rails: show form from another model in view

I have a location model and a post model. I want to allow the user to create new posts on the index page of the location model. How can I do it? I tried to make post new.html.erb partial in location index.html.erb , but this leads to errors since post new.html.erb refers to the @post variable.

How to do it?

+6
source share
3 answers

Try something like this

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

Now ur will have access to @post instance @post . Partial use of this

 render :partial => 'posts/post', :object => @post 
+14
source

You can run the form as follows:

 <%= form_for Post.new do ... %> 
+3
source

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

0
source

Source: https://habr.com/ru/post/895853/


All Articles