将数据从模型渲染到另一个控制器

时间:2016-10-12 05:48:04

标签: ruby-on-rails ruby-on-rails-4

我是ROR的新手。我实施了一个创建许可证的网站,然后将用户重定向到发票页面。我有一个用户模型,发票模型和许可证模型,它们在下面相互关联:

invoice.rb

class Invoice < ApplicationRecord
  belongs_to :user
  has_one :receipt
  belongs_to :permit
end

user.rb

class User < ApplicationRecord
  has_many :permits
  has_many :visitor_permits
  has_many :healthreports
  has_secure_password
end

permit.rb

class Permit < ApplicationRecord
  belongs_to :user
  has_one :invoice
end

我要做的是用户在申请许可证时填写所有详细信息,并且信息将保存到许可证模型中。之后,用户将重定向到发票页面,该页面呈现刚刚从许可证申请页面中的用户填写的所有数据,并从许可证模型中提取并显示在发票页面上,之后将再次保存到发票页面中。我不知道如何启动它。有什么想法怎么做?

我有许可证和发票控制器。如果您需要更多信息或我的代码随时告诉我。谢谢!

我尝试过在发票控制器NEW操作中创建一个与许可证模型链接的变量,这样我就可以使用它从Permit模型中渲染数据,然后将其保存到发票模型中。

class InvoiceController < ApplicationController
  def new
    @permits = Permit.find(params[:id])
    @invoice = Invoice.new
  end

  def create
    #@current_user = User.find_by(id: session[:user_id])
    @invoice = current_user.visitor_permits.invoices.build(invoice_params)
    if @invoice.save

    else

    end
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def invoice_params
    params.require(:invoice).permit(:vehicle_type, :name, :department, :carplate, :duration, :permitstart, :permitend, :price, :time)
  end
end

但我一直收到错误

ActiveRecord::RecordNotFound in InvoiceController#new
Couldn't find Permit with 'id'=

指向新行动。

@permits = Permit.find(params[:id])

这是我的permits_controller.rb,一旦按下按钮,它将在CREATE操作中重定向用户

class PermitsController < ApplicationController
  before_action :set_permit, only: [:destroy]
  def index
    @permits = Permit.where(:user_id => current_user.id)
  end

  def new
    @permits = Permit.new
  end

  def create
    @permits = current_user.permits.build(permit_params)
    if @permits.save
      redirect_to createinvoice_path(@permits)

    else
      render 'new'
    end
  end


  def destroy
    Permit.destroy_all(user_id: current_user)
    respond_to do |format|
      format.html { redirect_to root_path, notice: 'Permit was successfully canceled.' }
      format.json { head :no_content }
    end
  end

  def confirm
    @fields = %i[vehicle_type, carplate, studentid, name, department, permitstart, permitend]
    @permit = current_user.permits.build(permit_params)
    render :new and return unless @permit.valid?
  end

  def show
    @permits = Permit.find(params[:id])
  end

  def update
    @permits = Permit.where(user_id: current_user).take
    respond_to do |format|
      if @permits.update(permit_params)
        format.html { redirect_to root_path}
        flash[:success] = "Permit successfully updated"
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit
    @permits = Permit.find(params[:id])
    #@permits = Permit.find_or_initialize_by(user_id: params[:id])
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def permit_params
    params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
  end
end

Route.db

Rails.application.routes.draw do
  resources :users
  resources :permits do
    collection do
      post :confirm
    end
  end
  resources :visitor_permits do
    collection do
      post :confirm
    end
  end
  resources :invoice
  resources :receipt
  resources :health_report

  root 'static_pages#home'


  get 'permitapplication' => 'permits#new'
  get 'viewpermit' =>'permits#show'
  get 'show_permit' =>'permits#index'
  get 'invoice' => 'permits#invoice'
  post 'createpermit' => 'permits#create'
  post 'permits' => 'permits#create'

  post 'createvpermit' => 'visitor_permits#create'
  post 'vpermits' => 'visitor_permits#create'
  get 'show_vpermit' =>'visitor_permits#index'
  get 'show_visitor_permit' =>'visitor_permits#show'
  get 'visitorpermit' => 'visitor_permits#new'

  get 'createinvoice' => 'invoice#new'
  post 'invoice' => 'invoice#create'

  get 'new_health_report' => 'health_report#new'

  get 'payment' =>'transaction#new'

  get 'homepage/index'

  get 'adminlogin' => 'admin_controller#index'

  get 'patrollogin' => 'patrol_officer#index'

  post 'citations' => 'citations#create'
  get 'new_citation' => 'citations#new'
  get 'unpaid_citations' => 'citations#list_all'
  get 'edit_Citation' => 'citations#edit'

  get 'contact'=> 'static_pages#contact'
  get 'about' => 'static_pages#about'
  get 'help' => 'static_pages#help'

  get 'signup' => 'users#new'
  post 'users' => 'users#create'

  get 'login' => 'sessions#new' #Page for a new session
  post 'login' => 'sessions#create' #Create a new session
  delete 'logout'=>'sessions#destroy' #Delete a session


  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

1 个答案:

答案 0 :(得分:1)

我发现您的createinvoice路线未设置为接受任何额外参数。默认路由应该生成类似invoice/new的内容。因此,当在路径中发送@permit id时,您需要以下内容:

redirect_to createinvoice_path(permit_id: @permits.id)

并在invoice_controller抓取params[:permit_id]

编辑:     我不擅长解释,但我会尝试。 发票的路径路径为get createinvoice,因此,如果有人访问yourdomain.com/createinvoice,则会转到该页面。 请注意,您的路由get createinvoice不包含任何参数的占位符,您在rails默认生成的显示页面中看到的内容如get users/:id。如果路由设置是这样的话,那么当我们写redirect_to users_path(@user)时,rails会使用id的{​​{1}}并将其替换为@user,它将变为url当页面呈现时,如果用户标识为3,则在控制器中,您可以通过users/3访问该标识。但是在你的情况下,路由没有设置为接受任何参数,因此rails只是忽略了params[:id]中的@permits而你会收到错误,因为{{1}中没有createinvoice_path }}。 我建议的是id,如果许可证的ID是3,则会将网址设为params。因此,当您在Google中搜索时,这会将params作为查询参数发送。您将获得createinvoice_path(permit_id: @permits.id)中的createinvoice/?permit_id=3

希望你明白我的意思。