Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 链接到用户';s剖面图_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 链接到用户';s剖面图

Ruby on rails 链接到用户';s剖面图,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我如何链接到用户的配置文件视图,而不出现将Fixnum隐式转换为字符串的错误 我目前拥有的: - @questions.each do |question| = link_to question.user.username, "/users/"+question.user.id 基本上,我想要实现的是: 当用户点击链接时,他将被重定向到原始海报的个人资料页面。例如:localhost:3000/用户/1 家庭控制器: def profile if !user_signed_i

我如何链接到用户的配置文件视图,而不出现
将Fixnum隐式转换为字符串的
错误

我目前拥有的:

- @questions.each do |question|
  = link_to question.user.username,  "/users/"+question.user.id
基本上,我想要实现的是:

当用户点击链接时,他将被重定向到原始海报的个人资料页面。例如:localhost:3000/用户/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

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

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

我希望这会有帮助。

这个也应该有用

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