Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何创建链接到模型中_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何创建链接到模型中

Ruby on rails 如何创建链接到模型中,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我想知道我如何处理这个问题: 当我通过模型方法保存正文消息时,我想生成一个链接到正文消息中 比如说,在这个方法中,我创建了一条新消息,并希望在其中添加一个链接,这样当我的网站的用户浏览每一条消息时,都可以点击它 你会如何以一种好的方式做这件事?我试过了,但没用 Request.rb # Create a new Message after an acceptance def notify_acceptance msg = Message.new(subjects: [self.s

我想知道我如何处理这个问题:

当我通过模型方法保存正文消息时,我想生成一个链接到正文消息中

比如说,在这个方法中,我创建了一条新消息,并希望在其中添加一个链接,这样当我的网站的用户浏览每一条消息时,都可以点击它

你会如何以一种好的方式做这件事?我试过了,但没用

Request.rb

  # Create a new Message after an acceptance
  def notify_acceptance
    msg = Message.new(subjects: [self.subject], author: self.evaluated_by)
    msg.body = "a warm welcome to #{ActionView::Helpers.url_to_user_profile(self.requested_by)} who just joined us!"
    msg.distribute([self.subject], self.evaluated_by)
    return msg.save!
  end
这里是我的助手文件:

module RequestsHelper
  def url_to_user_profile(user)
    link_to user.name, profiles_path(user.id)
  end
end

谢谢

简单的答案是你不能<代码>链接到是从
ActionPack
创建的,其中as模型继承自
ActiveRecord

解决这个问题的一个简单方法是将
链接到
助手中的逻辑

例如:

app/views/index.html.erb

link_to_home
app/helpers/application_helper.rb

def link_to_home
    if root_url?
        "Home"
    elsif params[:controller] = "posts" && params[:action] == 'index'
        "Home"
    else
        link_to "Home", root_url
    end
end
这样,如果您在索引页上有分页,并且这是您的根url,它将不会链接到主页。但是如果您使用
link\u to\u,除非\u current
它将链接到home,即使它是执行
link\u to\u,除非\u current
的同一控制器和操作。如果这个例子没有意义,这并不重要。这是rails在helpers中保持
链接到
逻辑的方法

如果确实有必要,您可以在模型中创建一个包含常规HTML的字符串,然后在视图中转义它

class Post < ActiveRecord::Base
    def link
        "<a href="#{url}">#{title}</a>"
    end
end
class Post

然后在您的视图中,您可以拥有并将其链接到
url
,并将其命名为
title
,假设这些是您的
@post
对象上的属性,并且您保存了一些内容

您尝试过这种方法吗

链接到user.name、:controller=>“profiles”、:action=>“show”、:id=>user.id

因此:

link_到“您想要的字符串”、:controller=>“controllers name”、:action=>“action”、:(param name)=>value

在这里你可以插入你需要多少个参数

编辑

使用
profiles\u url(user.id)
可以获得此页面的url,您可以手动创建链接,如下所示:

msg.body =  "a warm welcome to <a href='#{profiles_url(self.requested_by.id)}'>#{profiles_url(self.requested_by.name)}</a> who just joined us!"
msg.body=“热烈欢迎刚刚加入我们的人!”

我认为问题在于您的型号(可能是用户)无法识别配置文件的路径(user.id), 所以在用户模型中添加以下内容

include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers
我不建议在模型中包含更多的视图帮助程序,您不能使用link\u to,但path helper方法可以工作,然后在msg中创建类似“link\u name”的url
它在Rails3中工作。

嗯。。。没有比html字符串更好的实践了?在模型中放置
link\u to
是不好的实践。link_to实际上是一个非常简单的助手,您可以在任何模型中构建自己。但大多数人不会把它放在这里,因为它不遵循MVC。不直接工作到模型中,因为方法的链接仅在ActionView中定义。是的,我注意到现在。。。但是你可以把这个消息放到一个查看页面,或者你可以调用profiles\u url并创建你自己的linkshow,即使它打破了mvc惯例,所以在我的应用程序中,我创建了一个PushMsg模型并将所有消息委托给这个模型,然后其他模型将遵循mvc规则