简体   繁体   English

从控制器传递实例变量以在Rails中进行查看

[英]Passing instance variable from controller to view in rails

I have a user profile controller called "userinfo" and it's corresponding view. 我有一个名为“ userinfo”的用户配置文件控制器,它是对应的视图。 The userinfo index is the root path. userinfo索引是根路径。 In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. 在首页(这是userinfo索引)中,我有一个链接,可以带您到用户个人资料页面。 It is giving me this error when I click on the image on the view page: 当我单击视图页面上的图像时,它给了我这个错误: 在此处输入图片说明 My routes are: 我的路线是: 在此处输入图片说明 My userinfos_controller: 我的userinfos_controller:

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

and my view is: 我的看法是:

<%= link_to image_tag("student.png", class: 'right'), userinfo_path(@userinfors) %>

I thought maybe I must include ':index' in the 'before_action :find_userinfo' at the top of my controller. 我以为也许我必须在控制器顶部的'before_action:find_userinfo'中包括':index'。 If I do that, the homepage doesn't even load and it gives me this error: 如果我这样做,主页甚至都不会加载,这给了我这个错误: 在此处输入图片说明

Try below code: 试试下面的代码:

controller 控制者

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

view 视图

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

Your problem is that you're trying to do perform a lookup based on something that's not an ActiveRecord (database) attribute. 您的问题是您试图基于非ActiveRecord(数据库)属性来执行查找。

Your root goes to UserinfosController which expects @userinformation_user_id but I can't tell from your code where that comes from. 您的根目录是UserinfosController ,它需要@userinformation_user_id但是我无法从您的代码中得知它的来源。

You need to define your route in order that this will be expecting for an specific param, maybe the user id , and then you're able to add the value within your view, in a link_to helper: 您需要定义路线,以便针对特定的参数(可能是用户id进行期望,然后可以在link_to帮助器中的视图内添加值:

You could modify your routes.rb to expect an id as param: 您可以修改您的routes.rb以将id作为参数:

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

Then in your controller, use a find to "find" in the database the user with such id. 然后,在您的控制器中,使用find在数据库中“找到”具有该ID的用户。 If you'd like to use where then that would give you a relationship with all the userinfos with the id being passed as param. 如果您想where使用where那将为您提供与所有userinfos的关系,其id作为参数传递。 If you want so, then use Userinfo.where('userinfo_id = ?', params[:id]) : 如果需要,请使用Userinfo.where('userinfo_id = ?', params[:id])

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

And then in your view you can access to @userinfors : 然后在您看来,您可以访问@userinfors

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

I think you could define the index to get all the userinfors and a show method to get an specific one, as you're trying to do. 我想您可以尝试定义index来获取所有userinfors和一个show方法来获取特定的userinfors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM