2017-05-27 6 views
-1

"userinfo"というユーザープロファイルコントローラがあり、それに対応するビューがあります。 userinfoインデックスはルートパスです。ホームページ(userinfoインデックス)には、ユーザープロフィールページにリンクするリンクがあります。 enter image description here マイuserinfos_controller:enter image description here 私のルートは以下のとおりです。コントローラからインスタンス変数をレールに渡す

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

    def index 
     @userinfors = Userinfo.where(:userinfo_id => @userinformation_user_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 image_tag("student.png", class: 'right'), userinfo_path(@userinfors) %> 

が、私は多分思った私は、ビュー・ページ上の画像をクリックしたとき、それは私に、このエラーを与えています私は ':index_'を 'before_action:find_userinfo'の中に入れてください。私はそれを行う場合は、ホームページにもロードされないと、それは私に、このエラーを与える:enter image description here

答えて

1

コードの下に試してみてください。

コントローラ

def index 
    @userinfors = Userinfo.where(:userinfo_id => @userinformation_user_id) #pass id instead of object @userinformation_user_id 
end 

ビュー

<% @userinfors.each do |u| %> 
    <%= link_to image_tag("student.png", class: 'right'), userinfo_path(u) %> 
<% end %> 
+0

お元気ですか、コントローラで何か変更しましたか?また、コントローラの「before_action」に「:index」を追加する必要がありますか? – Dinukaperera

+0

@Dinukapereraあなたはinstace変数の代わりにidを渡さなければなりませんuserinformation_user_id – puneet18

1

あなたの問題ActiveRecord(データベース)属性ではないものに基づいてルックアップを実行しようとしているということです。

ルートはUserinfosControllerになりますが、これは@userinformation_user_idとなりますが、どこから来たのか分かりません。

:あなたは、これは特定のparam、多分ユーザーidのために期待され、その後、あなたはlink_toヘルパーで、ビュー内の値を追加することができるしているようにするために、あなたのルートを定義する必要が

+0

あなたは混乱していますか?助けてくれてありがとう! – Dinukaperera

1

あなたはPARAMとしてidを期待するあなたのroutes.rbを変更できます。

get '/user_infors/:id', to: 'userinfos#index', as: 'userinfo_path' 

次に、あなたのコントローラで、データベースに、このようなIDを持つユーザーを「見つける」ためにfindを使用しています。 whereを使用する場合は、userinfosとの関係が得られ、idがparamとして渡されます。 あなたがそうしたい場合は、Userinfo.where('userinfo_id = ?', params[:id])を使用します。

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

そして、あなたのビューで、あなたは@userinforsにアクセスすることができます。

<% @userinfors.each do |user| %> 
    <%= link_to image_tag 'student.png', class: 'right', userinfo_path(user) %> 
<% end %> 

私はあなたがすべてのuserinforsshowを取得するためにindexを定義することができると思いますあなたがしようとしているように、特定のものを取得する方法。

+0

ありがとう、最後の行を理解できるように助けてくれますか? "特定のものを得るためのショー方法"?それ、どうやったら出来るの?また、コントローラーのbefore_actionに ":index"を追加する必要がありますか? – Dinukaperera

+0

そしてルートでは、なぜ '/ user_infors /:id'ですか?「user_infors」はどこから来たのですか? – Dinukaperera

+0

あなたが何らかのレコードを見つけようとしているときに、それを表示するには、それはshowメソッドで実行できる単なるレコードですが、使用できるすべてのレコードを表示したいのであればインデックスメソッド。 '/ user_infors'があなたのインデックスルートになります。'/user_infors /:id'は特定のレコードを取得することになります。 –

関連する問題