簡體   English   中英

如何從Rails的視圖文件中讀取參數?

[英]How to read parameters from view file in Rails?

我有一條路線:

http://127.0.0.1:3000/professor?roles=1

現在,在我的視圖文件中,我有一個復選框,我想檢查是否Roles = 1我希望選中該復選框,如果沒有選中,則必須取消選中該復選框。

我嘗試了這個:

<input type="checkbox" <%=  (!@roles.blank? && !@roles.include?(0) ? " checked='checked' " : "") %> />

沒有錯誤,但是沒有用。

查詢參數在params哈希中可用:

# http://127.0.0.1:3000/professor?roles=1
params[:role] #=> "1"

請注意,這些值是字符串。

還有一個check_box_tag幫助器:

<%= check_box_tag nil, nil, params[:role] == "1" %>

您還可以在控制器操作中設置實例變量:

def index
  @role = params[:role].to_i
  # ...
end

並在您的視圖中使用它:

<%= check_box_tag nil, nil, @role == 1 %>

暫無
暫無

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

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