2017-05-27 191 views
0

我有一个名为“userinfo”的用户配置文件控制器,它是相应的视图。 userinfo索引是根路径。在主页(这是用户信息索引),我有一个链接,可以将您带到用户个人资料页面。它给我这个错误,当我去到主页:enter image description here为什么在尝试访问rails中的实例时遇到recordnotfound错误?

我的路线是:enter image description here 我userinfos_controller:

class UserinfosController < ApplicationController 
    before_action :find_userinfo, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    def index 
     @userinfors = Userinfo.find(params[:id]) 
    end 

    def show 
     @myvideo = Video.last 
    end 

    def new 
     @userinformation = current_user.userinfos.build 
    end 

    def create 
     @userinformation = current_user.userinfos.build(userinfo_params) 
     if @userinformation.save 
      redirect_to root_path 
     else 
      render 'new' 
     end 
    end 

    def edit 
    end 

    def update 
    end 

    def destroy 
     @userinformation.destroy 
     redirect_to userinfo_path 
    end 

    private 
     def userinfo_params 
      params.require(:userinfo).permit(:name, :email, :college, :gpa, :major) 
     end 

     def find_userinfo 
      @userinformation = Userinfo.find(params[:id]) 
     end 
end 

,我的看法是:

<%= link_to 'profile', userinfors_path(@userinfors) %> 

我的路线。 rb文件:

Rails.application.routes.draw do 
    devise_for :users 
    resources :userinfos do 
    resources :videos 
    end 
    resources :pages 
    get '/application/decide' => 'application#decide' 
    root 'userinfos#index' 
    get '/userinfos/:id', to: 'userinfos#show', as: 'userinfors' 
end 

感谢您的任何帮助!

回答

1

好的,有多个错误,你没有遵循rails的惯例,index不是你所使用的。 Index用于列出所有用户,show用于特定的用户id通过params

你的索引路径是,你可以看到,/userinfos这是正确的,它不会有任何id但你正在努力寻找userparams[:id]这是nil,因此错误。

让我们试试这个:

def index 
    @userinfors = Userinfo.all #pagination is recommended 
end 

在索引视图,

<% @userinfors.each do |userinfor| %> 
    <%= link_to "#{userinfor.name}'s profile", userinfo_path(userinfor) %> 
<% end %> 

应该现在的工作。

请仔细阅读routingaction controller,才能认识和了解魔术背后轨路由MVC架构 ..

+0

谢谢!我试图使用快捷方式,因为我不想在不同控制器之间创建关联。但是,你的方式是有效的。 – Dinukaperera

+0

你可以根据你的需要绝对地操作导轨,但是需要理解标准的方法..不管怎样,很高兴它有助于.. :) –

相关问题