【问题标题】:Link to user's profile view链接到用户的个人资料视图
【发布时间】:2016-04-02 23:38:50
【问题描述】:

如何链接到用户的个人资料视图而不出现no implicit conversion of Fixnum into String 错误?

我目前拥有的:

- @questions.each do |question|
  = link_to question.user.username,  "/users/"+question.user.id

基本上我想要实现的是:

当用户点击链接时,他将被重定向到原始发帖者的个人资料页面。例如:localhost:3000/users/1

家庭控制器:

  def profile
    if !user_signed_in?
        redirect_to new_user_session_path
    end
    if User.find_by_id(params[:id])
      @user = User.find(params[:id])
    else
      redirect_to root_path
    end
  end

路线:

Rails.application.routes.draw do
  root 'home#home'
  devise_for :users
  get "/users/:id" => "home#profile"
  get "home" => "home#feed"
  get "questions" => "home#questions"
  resources :questions
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    您需要手动将 id 转换为字符串:

      = link_to question.user.username,  "/users/"+question.user.id.to_s
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      这个应该也可以。

      = link_to question.user.username,  "/users/#{question.user.id}"
      

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 2012-01-20
        • 2012-09-06
        • 2011-09-14
        • 2018-09-27
        • 2013-04-29
        • 2014-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多