简体   繁体   中英

Passing instance variable from controller to view in rails

I have a user profile controller called "userinfo" and it's corresponding view. The userinfo index is the root path. In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. It is giving me this error when I click on the image on the view page: 在此处输入图片说明 My routes are: 在此处输入图片说明 My 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. 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.

Your root goes to UserinfosController which expects @userinformation_user_id but I can't tell from your code where that comes from.

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:

You could modify your routes.rb to expect an id as param:

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. 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. If you want so, then use 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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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