Rails表示相关的模型

时间:2018-03-28 15:52:53

标签: ruby-on-rails

我正在为大学创建一个耐心的应用程序。我需要为患者报告创建一个表单。

患者和报告有关系

我的模特

用户,患者和报告模型:

    class Patient < ActiveRecord::Base

      include Tire::Model::Search
      include Tire::Model::Callbacks

      validates :name, presence:true
      validates :age, presence:true
      validates :phone, presence:true
      validates :ailment, presence:true
      validates :apointment, presence:true
      validates :status, presence:true

      belongs_to :user
      has_many :reports, dependent: :destroy

      def to_s
        name
      end
    end

    class Report < ActiveRecord::Base
      belongs_to :user
      belongs_to :patient
    end

    class User < ActiveRecord::Base
      include Clearance::User

      has_many :patients
      has_many :reports, dependent: :destroy
    end

路线:

           reports GET    /reports(.:format)                      reports#index
                   POST   /reports(.:format)                      reports#create
        new_report GET    /reports/new(.:format)                  reports#new
       edit_report GET    /reports/:id/edit(.:format)             reports#edit
            report GET    /reports/:id(.:format)                  reports#show
                   PATCH  /reports/:id(.:format)                  reports#update
                   PUT    /reports/:id(.:format)                  reports#update
                   DELETE /reports/:id(.:format)                  reports#destroy

这是报告控制器

class ReportsController < ApplicationController

    before_action :find_patient
    before_action :find_report, only: [:show, :edit, :update, :destroy]
    before_action :require_login
    def new
        @reports = Report.new 
    end 

    def show 
    end

    def create
        @report = Report.new(report_params)
        @report.user_id = current_user_id
        @report.patient_id = @patient.id

        if @report.save
            redirect_to patient_path(@patient)
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @report.update(report_params)
            redirect_to patient_path(@patient)
        else 
            render 'edit'
        end
    end

    def destroy
        @report.destroy
        redirect_to patient_path(@patient)
    end

    private

        def report_params
            params.require(:report).permit(:date, :report)
        end

        def find_patient
            @patient = Patient.find(params[:patient_id])
        end

        def find_report
            @report = Report.find(params[:id])
        end
end

我的表格

我认为这是我的问题。我不确定在@之后我应该放什么来允许为特定用户形成一个报告。 这里有任何建议。

<%= form_for @report  do |f| %>
    <%= f.label :date %>
    <%= f.text_field :date %>

<% end %>

链接到访问表单页面

<% if signed_in? %>
        <% if @patient.id == current_user.id %>
            <%= link_to "Add Visit Report", new_report_path(patient_id: @patient.id),class: "button_one" %>
        <% end %>
    <% end %>

1 个答案:

答案 0 :(得分:0)

我建议你使用嵌套资源。所以你的路线看起来与此类似:

resources :patients do
  resources :reports
end

所以路线如下

             Prefix Verb   URI Pattern                                      Controller#Action
    patient_reports GET    /patients/:patient_id/reports(.:format)          reports#index
                    POST   /patients/:patient_id/reports(.:format)          reports#create
 new_patient_report GET    /patients/:patient_id/reports/new(.:format)      reports#new
edit_patient_report GET    /patients/:patient_id/reports/:id/edit(.:format) reports#edit
     patient_report GET    /patients/:patient_id/reports/:id(.:format)      reports#show
                    PATCH  /patients/:patient_id/reports/:id(.:format)      reports#update
                    PUT    /patients/:patient_id/reports/:id(.:format)      reports#update
                    DELETE /patients/:patient_id/reports/:id(.:format)      reports#destroy
           patients GET    /patients(.:format)                              patients#index
                    POST   /patients(.:format)                              patients#create
        new_patient GET    /patients/new(.:format)                          patients#new
       edit_patient GET    /patients/:id/edit(.:format)                     patients#edit
            patient GET    /patients/:id(.:format)                          patients#show
                    PATCH  /patients/:id(.:format)                          patients#update
                    PUT    /patients/:id(.:format)                          patients#update
                    DELETE /patients/:id(.:format)                          patients#destroy

然后按如下方式修改控制器操作

def new
    @report = @patient.reports.build 
end 

def create
    @report = @patient.reports.build 
    if @report.save
        redirect_to patient_path(@patient)
    else
        render 'new'
    end
end

def find_report
    @report = @patient.reports.find(params[:id])
end

并且视图将如下所示

<%= form_for [@patient, @report]  do |f| %>
    <%= f.hidden_field :user_id, value: current_user.id %>
    <%= f.label :date %>
    <%= f.text_field :date %>
<% end %>

我希望这能解决你的问题