Rails: Show form from different model in a view

前端 未结 3 465
无人共我
无人共我 2021-01-12 20:39

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 l

相关标签:
3条回答
  • 2021-01-12 21:28

    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
    
    0 讨论(0)
  • 2021-01-12 21:29

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

    0 讨论(0)
  • 2021-01-12 21:33

    You can start the form like this:

    <%= form_for Post.new do ... %>
    
    0 讨论(0)
提交回复
热议问题