Rails: Render a View (not a partial) From Within a View

前端 未结 4 1328
一生所求
一生所求 2021-02-05 00:30

I have a controller that responds to both html and js. The html view renders the whole page (including the header and footer), while the <

4条回答
  •  你的背包
    2021-02-05 00:57

    Rendering a non-partial view inside another view isn't exactly the Rails Way™. Your current solution is probably better, and not an uncommon approach. Rename it _body, or something else appropriate, if you feel weird about the the partial name being the same as the action.

    However if your view can be shared, as it seems like it could in this case, you could just make it a layout.

    This is facilitated by the fact that, somewhat against the principle of least surprise, Rails will render an html template for a js action if no js template exists. This means that you could remove both the js template, and the partial, and just create a layout entitled, for example, fadein.js.erb:

    # yourviews/show.html.erb
    
    Content!
    # layouts/fadein.js.erb $("#main").fadeIn("<%= escape_javascript(yield) %>"); # YourController.rb def show # ... respond_to do |wants| wants.html wants.js { render :layout => "fadein" } end end

提交回复
热议问题