【问题标题】:Link_to to the same page?Link_to 到同一页面?
【发布时间】:2021-05-11 13:52:37
【问题描述】:

我是 Ruby on Rails 的新手,感谢您的指导。

我的 show.html.haml 中有以下行。现在,当我单击在显示页面中创建的链接时,它会加载一个指向对话消息路径的新页面。我不想加载新页面,而是希望在我的 show.html.haml 页面中的一个列中看到这个页面,Link_to 是其中的一部分。有点像偏心!

= link_to (conversation.messages.last.body),conversation_messages_path(conversation),class: "last-message"

提前谢谢你。

【问题讨论】:

  • 非常不清楚“发送页面”应该是什么意思,或者“在同一页面的列中查看对话消息路径页面(show.html.haml)”是什么意思。这听起来有点像您在询问 AJAX,这是整本书 or guide 的主题,而不是 Stackoverflow 的答案。也可能是partials...请通过编辑您的问题来增加清晰度而不是评论来回复。
  • @max 我更新了我的帖子,希望现在更清楚了。

标签: ruby-on-rails


【解决方案1】:

这段代码有你的想法吗?

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

运行方式:转到root_path:127.0.0.1:3000,然后点击link_to部分,它会重新加载页面,显示次数点击链接。

一切都很容易理解,注意params.permit(:flag).merge(:flag =&gt; true) 允许变量标志在将其值发送到服务器之前。

编码愉快! : D

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多