在Link_to块中使用Rails简单的锚点

时间:2018-12-02 21:10:38

标签: ruby-on-rails ruby-on-rails-5 anchor link-to

我看过一百篇关于如何在rails的常规链接上添加锚标记的文章,但没有一篇关于使用link_to做的块上使用的格式的文章。我知道我缺少一些愚蠢的东西,但是应该怎么做?如果我想一直跳转到#selected-card的div ID上的锚点,则锚点格式类似于以下代码块?任何帮助都将不胜感激...

index.html.erb(带有“显示”页面的链接)

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.properties import BooleanProperty
from kivy.graphics import *

class CliClicker(BoxLayout):

    blue = ObjectProperty(True)
    red = ObjectProperty(False)
    green = ObjectProperty(False)

    def bg_change(self):
        #Change background according value set in radio buttons
        if self.blue:
            print('color changed to blue')
            with self.menuoptions.canvas:
                Color(rgba=(.7,.7,.9,1))
        elif self.red:
            print('color changed to red')
            with self.menuoptions.canvas:
                Color(rgba=(.9,.7,.7,1))
        elif self.green:
            print('color changed to green')
            with self.menuoptions.canvas:
                Color(rgba=(.7,.9,.7,1))

    def show_value(self, instance, value, box):
        self.value = box
        print(instance, box, self.value)
        print('blue', self.blue)
        print('red', self.red)
        print('green', self.green)

class MainApp(App):

    def build(self):
        return CliClicker()

app = MainApp()
app.run()

我已经尝试过<%= link_to发布,锚点:“选定的卡片”可以做%>和其他一百万种事情,但似乎无法使其正常工作。

Show.html.erb(我的锚ID所在的位置):

<OptionButton@Button>:
    size_hint: (None,None)
    width: 200
    height: 40

<CliClicker>:
    orientation: "vertical"
    id: cliclicker
    menuoptions: menuopts

    TabbedPanel:
        do_default_tab: False
        height: 200
        id: tabs

        TabbedPanelItem:
            text: "Menu"
            id: menu

            FloatLayout:
                id: menuopts
                canvas:
                    Color:
                        rgba: .7,.7,.9,1
                    Rectangle:
                        pos: self.pos
                        size: self.size

                OptionButton:
                    text: 'Option 1'
                    pos_hint: {'right':.63, 'top':.9}

                OptionButton:
                    text: 'Option 1'
                    pos_hint: {'right':.63, 'top':.8}

                GridLayout:
                    cols: 6
                    size_hint: (None, None)
                    pos_hint: {'top': .7, 'right':.69}
                    height: 40
                    width: 300

                    canvas:
                        Color:
                            rgba: .9,.9,.9,1
                        Rectangle:
                            pos: self.pos
                            size: self.size

                    CheckBox:
                        group: "bg_color"
                        value: root.blue
                        on_active: cliclicker.show_value(self, self.value, self.active)
                        color: 0,0,0,1
                    Label:
                        text: 'blue'
                        color: 0,0,0,1
                    CheckBox:
                        group: "bg_color"
                        value: root.red
                        on_active: cliclicker.show_value(self, self.value, self.active)
                        color: 0,0,0,1
                    Label:
                        text: 'red'
                        color: 0,0,0,1
                    CheckBox:
                        group: "bg_color"
                        value: root.green
                        on_active: cliclicker.show_value(self, self.value, self.active)  
                        color: 0,0,0,1
                    Label:
                        text: 'green'
                        color: 0,0,0,1

路由文件:

<%= link_to post do %>
  <%= image_tag(post.postimage_url, :alt => "logo", :style=>"box-shadow: 0px 0px 2px #ddd; margin-bottom: 8px; width: 100%; max-height: 140px;") %>
<% end %>

1 个答案:

答案 0 :(得分:2)

如果使用对象代替完整路径,则需要使用html选项将其嵌入数组(source)内:

<%= link_to [post, anchor: "selected-card"] do %>
  ...
<% end %>
<!-- generates <a href="/posts/1#selected-card">...</a> -->