[英]Link_to to the same page?
我是 Ruby on Rails 的新手,感謝您的指導。
我的 show.html.haml 中有以下行。 現在,當我單擊在顯示頁面中創建的鏈接時,它會加載一個指向對話消息路徑的新頁面。 我不想加載新頁面,而是希望在我的 show.html.haml 頁面中的一個列中看到此頁面,Link_to 是其中的一部分。 像偏的東西!
= link_to (conversation.messages.last.body),conversation_messages_path(conversation),class: "last-message"
先感謝您。
這段代碼有你的想法嗎?
config/routes.rb
Rails.application.routes.draw do
get "/home", to: "pages#home"
root to: "pages#show"
end
app/views/pages/show.html.haml
= link_to "Message from this page. Click to go!", root_path(params.permit(:flag).merge(:flag => true))
- if @flagCount
%form{ :action => "/save", :method => "post" }
%fieldset
%label{:for => "title"} This is #{@indexCount} time:
%input{:name => "title", :type => "text", :value => ""}/
%input{:type => "submit" }
- else
Nothing. This is #{@indexCount} time.
%p This line is variables information: @flagCount = #{@flagCount}; @indexCount = #{@indexCount}
app/controllers/pages_controller.rb
class PagesController < ApplicationController
# before_action :accept_all_params
@@staticCount = 1
@@staticFlag = false
def home
end
def show
if params[:flag].present?
@@staticFlag = ActiveModel::Type::Boolean.new.cast(params[:flag])
if @@staticFlag
@@staticCount = @@staticCount + 1
else
@@staticCount = 1
@@staticFlag = false
end
else
@@staticCount = 1
@@staticFlag = false
end
@indexCount = @@staticCount
@flagCount = @@staticFlag
end
private
# permit all parameters
# def accept_all_params
# params.permit!
# end
end
運行方式: Go 到 root_path: 127.0.0.1:3000
,然后點擊到 link_to 部分,它將重新加載頁面,顯示計數次數點擊鏈接。
一切都很容易理解,在將其值發送到服務器之前,請注意params.permit(:flag).merge(:flag => true)
行允許變量標志。
快樂編碼::D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.