开发者

Rails: Keeping a partial DRY when using locals

开发者 https://www.devze.com 2023-03-05 12:47 出处:网络
I have partial that is being reused by a couple controllers. Right now I have this partial set up to accept 2 objects which I don\'t want to do.

I have partial that is being reused by a couple controllers.

Right now I have this partial set up to accept 2 objects which I don't want to do.

-if @article
  .main-inner
    .article-header
      .article-title
        =@article.title
      -if !@article.byline.blank?
        %span#byline
          =@article.byline
      %span#timestamp
        =@article.publish_date.strftime("%a, %b %d, %Y %I:%M %Z")
    .article-content
      =truncate(@article.content, :length => 600).html_safe
-else
  .main-inner
    .article-header
      .article-title
        =article.title
      -if !article.byline.blank?
        %span#byline
          =article.byline
      %span#timestamp
        =article.publish_date.strftime("%a, %b %d, %Y %I:%M %Z")
    .article-content
      =truncate(article.content, :length => 600).html_safe

When @article

def show
  @article = Article.find_by_permalink(params[:permalink])
end

When through locals

$("#article").html("<%= escape_javascript render(:partial => 'article/view', :locals => { :article => @newarticle }) %>")

1) How can I consolidate my partial to no开发者_如何学Got use if @article.

2) In my js. What is the proper syntax for rendering partials so I don't have to say :partial => 'article/view' and use :locals?


if it is a partial then you should use article(local) instead of @article(not instant variable), and should always pass it in locals when render the partial.

render(:partial => 'article/view', :locals => { :article => @article })

or

render(:partial => 'article/view', :locals => { :article => @newarticle })
0

精彩评论

暂无评论...
验证码 换一张
取 消