2014-09-01 48 views
0

我想創建一個紅寶石的方法來處理我的超薄博客文章塊引用。現在,我有以下視圖助手:中間人blockquote紅寶石功能

def blockquote(content,author,source=nil,source_link=nil) 
    data = ' 
     <blockquote> 
     <p>'+content.html_safe+'</p> 
     <footer> 
     <strong>'+author+'</strong>' 

    if source && source_link 
     data = data + ' 
     <cite> 
      <a href="'+source_link+'">'+source+'</a> 
     </cite> 
     ' 
    end 

    data = data + '</footer></blockquote>' 
    return data 
    end 

這對於像帖子工程..

= blockquote("When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again.","Steve Jobs, Apple Computers") 

然而,會有一個更好的辦法?我想包裝的文字像

- blockquote 
    When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again. 

但我需要一種方式來傳遞作者,並選擇源+源鏈接。

當前的方法還需要報價在一條線上,而不是多條線。思考?

回答

2

我會創建一個quote_template部分,而不是像你這樣編碼html。然後,只需做這樣的事情:

def blockquote(options={}) 
    ... #checks and business logic 
    render partial: 'quote_template', options 
    end 

你可以初始化它想:

blockquote({ 
    author: "Steve Jobs", 
    quote: "Some awesome quote...", 
    ... 
    }) 

_quote_template.html.erb部分可能是這個樣子:

<blockquote> 
    <p><%= options[:quote] %></p> 

    <footer> 
    <strong><%= options[:author] %></strong> 

    <% if options[:source] %> 
     <cite> 
     <a href="<%= options[:source_link] %>"><%= options[:source] %></a> 
     </cite> 
    <% end %> 
    </footer> 
</blockquote> 
+0

是的,這正是,我一直在尋找,謝謝! – 2014-09-02 02:49:13

+0

我現在得到這個錯誤:== Middleman正在加載 /Users/chrishough/BusinessNoConformity/CodeNoConformity/Blog/config.rb:2:in'require':/ Users/chrishough/BusinessNoConformity/CodeNoConformity/Blog/lib /助手/ view_helpers.rb:23:語法錯誤,意外的'\ n',期待=>(SyntaxError) – 2014-09-02 04:31:21

+0

只是問一個新的問題,如果你仍然需要幫助:) – fyz 2014-09-02 22:32:48

2

我創建了以下超薄模板我的偏好目錄:

blockquote 
    p 
    = locals[:quote] 

    footer> 
    strong 
     = locals[:author] 

    - if locals[:source] 
     cite 
     a[href="#{locals[:source_link]}"] 
      = locals[:source] 

可以通過中間人的諧音被稱爲像:

= partial "components/blockquote", locals: {\ 
    author: "Steve Jobs, Apple Computers",\ 
    quote: "When you grow up you tend to get told that the world is the way it is and you're life is just to live your life inside the world. Try not to bash into the walls too much. Try to have a nice family life, have fun, save a little money. That's a very limited life. Life can be much broader once you discover one simple fact: Everything around you that you call life was made up by people that were no smarter than you. And you can change it, you can influence it… Once you learn that, you'll never be the same again."} 
+0

感謝分享。不要忘記接受答案,並高舉對方的回答。 – 2014-09-03 22:57:17

+0

正是我在找的!努力從中間人幫手方法得到部分渲染,而它不會這樣做。 – reid 2014-12-19 00:42:48