簡體   English   中英

如何通過超鏈接調用方法?

[英]How to call method via hyperlink?

如何在index.html.erb上調用帶有超鏈接的方法?

我目前有一個搜索字段,可讓用戶輸入關鍵字。 這將搜索模型中的屬性並顯示找到的任何屬性。

例如,我在其中有records_controller.rb:

def index
  @records = Record.search(params[:search])
end

app / models / record.rb具有:

  # Added for the search 
  def self.search(search)
    where("title LIKE ? OR keyword LIKE ? OR description LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
  end

“標題”,“關鍵字”和“描述”是模型中的屬性。

app / views / records / index.html.erb包含一個搜索表單:

<%= form_tag(records_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: " Search" %>
<%= submit_tag "Find" %>
<% end %>

在同一index.html.erb頁面上,如何添加超鏈接以顯示來自包含指定標題的模型的所有數據?

例如顯示標題為“ truck”的所有行。 如果用戶單擊超鏈接,它將顯示數據

where('title LIKE "truck"', "%#{search}%")

我假設我還需要在記錄模型中放置一個新方法,例如:

# I'll have to play with this because I know it is wrong
# Added for the search 
def self.searchtruck(search)
  where('title LIKE "truck"', "%#{search}%")
end

這可以滿足您的要求,並創建帶有param[:search] = 'truck'

<%= link_to 'Search for truck', records_path(search: 'truck') %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM