简体   繁体   English

Rails:如何从视图访问关系属性

[英]Rails : How to accessing relation attributes from the view

I have the following rails code 我有以下Rails代码

Employee model: id | 员工模型:id | emp_name | emp_name | emp_number emp_number

class Employee < ActiveRecord::Base

    has_many :visitors
end

Visitor Model:id|employee_id|visitor_name|vis_company|vis|email 访客模型:id |员工ID |访客名称| vis_company | vis |电子邮件

    class Visitor < ActiveRecord::Base
    belongs_to :employee
end

Employee Controller : 员工控制人:

class EmployeesController < ApplicationController
  before_action :set_employee, only: [:show, :edit, :update, :destroy]

  # GET /employees
  # GET /employees.json
  def index
    @employees = Employee.all
  end

  # GET /employees/1
  # GET /employees/1.json
  def show
  end

  # GET /employees/new
  def new
    @employee = Employee.new
  end

  # GET /employees/1/edit
  def edit
  end

  # POST /employees
  # POST /employees.json
  def create
    @employee = Employee.new(employee_params)

    respond_to do |format|
      if @employee.save
        format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
        format.json { render :show, status: :created, location: @employee }
      else
        format.html { render :new }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /employees/1
  # PATCH/PUT /employees/1.json
  def update
    respond_to do |format|
      if @employee.update(employee_params)
        format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
        format.json { render :show, status: :ok, location: @employee }
      else
        format.html { render :edit }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /employees/1
  # DELETE /employees/1.json
  def destroy
    @employee.destroy
    respond_to do |format|
      format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_employee
      @employee = Employee.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def employee_params
      params.require(:employee).permit(:emp_id, :emp_name, :emp_email, :emp_phone, :emp_mobile)
    end
end

Visitor Controller : 访客控制器:

class VisitorsController < ApplicationController
  before_action :set_visitor, only: [:show, :edit, :update, :destroy]

  # GET /visitors
  # GET /visitors.json
  def index
    #@visitors = Visitor.find(:all, :order => 'emp_name')
    #@visitors = Visitor.all.includes(:emp_name)
    @visitors = Visitor.all
    #@employees = @visitors.Employee.find(:all, :order => 'emp_name')
        #@employees = @visitors.employee :include => [:emp_name]
  end

  # GET /visitors/1
  # GET /visitors/1.json
  def show
  end

  # GET /visitors/new
  def new
    @visitor = Visitor.new
  end

  # GET /visitors/1/edit
  def edit
  end

  # POST /visitors
  # POST /visitors.json
  def create
    @visitor = Visitor.new(visitor_params)

    respond_to do |format|
      if @visitor.save
        format.html { redirect_to @visitor, notice: 'Visitor was successfully created.' }
        format.json { render :show, status: :created, location: @visitor }
      else
        format.html { render :new }
        format.json { render json: @visitor.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /visitors/1
  # PATCH/PUT /visitors/1.json
  def update
    respond_to do |format|
      if @visitor.update(visitor_params)
        format.html { redirect_to @visitor, notice: 'Visitor was successfully updated.' }
        format.json { render :show, status: :ok, location: @visitor }
      else
        format.html { render :edit }
        format.json { render json: @visitor.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /visitors/1
  # DELETE /visitors/1.json
  def destroy
    @visitor.destroy
    respond_to do |format|
      format.html { redirect_to visitors_url, notice: 'Visitor was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_visitor
      @visitor = Visitor.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def visitor_params
      params.require(:visitor).permit(:vis_id, :vis_name, :vis_email, :vis_company, :employee_id)
    end
end

Now my main problem is that I cant access employee name from visitor view : 现在我的主要问题是我无法从访问者视图访问员工姓名:

<p id="notice"><%= notice %></p>

<h1>Listing Visitors</h1>

<table>
  <thead>
    <tr>
      <th>Vis</th>
      <th>Vis name</th>
      <th>Vis email</th>
      <th>Vis company</th>
      <th>Visitor Host</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @visitors.each do |visitor| %>
      <tr>
        <td><%= visitor.id %></td>
        <td><%= visitor.vis_name %></td>
        <td><%= visitor.vis_email %></td>
        <td><%= visitor.vis_company %></td>
        <td><%= visitor.employee.emp_name %></td>
        <td><%= link_to 'Show', visitor %></td>
        <td><%= link_to 'Edit', edit_visitor_path(visitor) %></td>
        <td><%= link_to 'Destroy', visitor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Visitor', new_visitor_path %>

I created new project from the scratch and for some reason it started working. 我从头开始创建了一个新项目,由于某种原因它开始起作用。 My mistake was that I didn't define the relation from the start. 我的错误是我从一开始就没有定义关系。 I added employee_id after everything else is created I think the rails didn't build the relation at that time. 在创建所有其他内容之后,我添加了employee_id,我认为Rails当时并未建立关系。 thanks everyone 感谢大家

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

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