重构到helper相关的笔记记录

在helper重构view的代码,遇到包了很多层的html可以用字串的方式处理

def render_vote_icon(work)
    if work.work_voter.include?(current_user) && work.competition.get_on?
       "<span class='vote_counts_span'><i class='fa fa-thumbs-up' aria-hidden='true'></i> #{@work.vote_counts}</span>".html_safe
    else
       "<span class='vote_counts_span'><i class='fa fa-thumbs-o-up' aria-hidden='true'></i> #{@work.vote_counts}</span>".html_safe
    end
  end

要注意的点:

  • 字串要用双引号包起来
  • 外面是双引号的话,里面要用改成单引号(双引号包单引号,或者单引号包双引号)
  • 中间的ruby语言用#{}包起来
  • 最后一定要加.html_safe

helper相关

def render_vote_button(work, user)
    if work.competition.get_on?
      if work.work_voter.include?(user)
        link_to("感谢支持", "#" ,class:"btn vote-btn-lg disabled")
      else
        link_to('#', class:"btn vote-btn-lg", id:"upvote-btn") do
          content_tag(:i, "", class: "fa fa-thumbs-o-up", :'aria-hidden' => true)+"支持他"
        end
      end
    end
  end
  • 搜索关键词 rails content tag aria
  • +左右都是字串,两个字串组合要用+
    ps:
    link_to("work_path(work)+params", "data-toggle" => "modal", 'data-target' => '#workModal', remote: true) do

  • content_tag里面有一个空字符串,是因为要严格按照api里查到的格式来写,不然会报错


helper相关,重构到helper出现了一个问题。在html的时候有两个按钮,refactor到helper之后(不加result)只剩一个按钮。

原因:html是边读取边加载到网页的,而使用helper,则是先读取判断,最后回传资料(回传的是最后的结果)。而不加result的话,最后是读取到link_to("查看全部作品", competition_path(competition), class:"vote-btn")。所以这段代码以上的资料只是作为逻辑存在,最后回传的是这段代码。所以只会显示一个按钮。

解决方法:用result的方法,result加到的地方会回传,所以两个按钮就都可以显示了。

ps:因为加了result,这是个字段。所以需要在最后加上html_safe

  • view

    <% if Competition.last.present? %>
    <% if Competition.last.end? %>
    <%= link_to("查看获奖作品", awards_competition_path(Competition.last) ,class:"work-btn") %>
    <% else %>
    <% if current_user && current_user.valid_member? %>
    <%= link_to("我要参赛", new_competition_work_path(Competition.last) ,class:"work-btn") %>
    <% else %>
    <%= link_to("我要参赛", "#", class:"work-btn", "data-toggle" =>"modal", "data-target" => "#tips") %>
    <% end %>
    <% end %>
    <%= link_to("查看全部作品", competition_path(Competition.last) ,class:"vote-btn") %>
    <% else %>
    比赛还未开始,请耐心等候
    <% end %>

  • helper

    def render_competition_button_change(competition, user)
    result = ""
    if competition.present?
    if competition.end?
    result += link_to("查看获奖作品", awards_competition_path(competition) ,class:"work-btn")
    else
    if user && user.valid_member?
    result += link_to("我要参赛", new_competition_work_path(competition), class:"work-btn")
    else
    result += link_to("我要参赛", "#", class:"work-btn", "data-toggle" =>"modal", "data-target" => "#tips")
    end
    end
    result += link_to("查看全部作品", competition_path(competition), class:"vote-btn")
    else
    result += "比赛还未开始,请耐心等候"
    end
    result.html_safe
    end


helper里面是可以套helper的,不过调取套用的helper时也是要传参

def render_competition_button_change(competition, user)
    result = ""
     if competition.present?
       if competition.end?
         result += link_to("查看获奖作品", awards_competition_path(competition) ,class:"work-btn")
       else
         result += render_participate_competition_button(user, competition)
       end
       result += link_to("查看全部作品", competition_path(competition), class:"vote-btn")
     else
        result += "比赛还未开始,请耐心等候"
     end
     result.html_safe
  end

  • 正确

link_to('#', class:"btn vote-btn-lg", id:"upvote-btn")

  • 错误(多了一个空格键)

link_to ('#', class:"btn vote-btn-lg", id:"upvote-btn")