RoR - 如何查询数据库中的用户输入

时间:2016-07-18 03:40:36

标签: ruby-on-rails

所以,我正在创建一个RoR应用程序,我有管理员用户可以编辑产品列表和价目表,我有常规用户可以创建,编辑和查看报价。

我的模型看起来像这样:



class User < ApplicationRecord
  has_many :quotes
end
&#13;
&#13;
&#13;

&#13;
&#13;
class Product < ApplicationRecord
    has_many :prices
    has_many :relationships
    has_many :quotes, through: :relationships
    validates :name, presence: true
    validates :description, presence: true
end
&#13;
&#13;
&#13;

&#13;
&#13;
class Price < ApplicationRecord
  belongs_to :product
end
&#13;
&#13;
&#13;

&#13;
&#13;
class Relationship < ApplicationRecord
    belongs_to :product
    belongs_to :quote
    validates :product_id, presence: true
end
&#13;
&#13;
&#13;

编辑 - 报价模型:

&#13;
&#13;
class Quote < ApplicationRecord
  belongs_to :user, optional: true
  has_many :relationships
  has_many :products, through: :relationships
  has_many :prices, through: :products
  default_scope -> { order(created_at: :desc) }
  validates :t_name, presence: true
  validates :b_name, presence: true
  validates :maturity, presence: true, numericality: { only_integer: true }
end
&#13;
&#13;
&#13;

我的报价控制器看起来像这样:

&#13;
&#13;
class QuotesController < ApplicationController
  before_action :logged_in_user
  before_filter :authorize_admin, only: [:index]
  
  def index
    @quotes = Quote.all
    @products = Product.all
  end

  def show
    @quote = Quote.find(params[:id])
    @products = Product.all
  end

  def new
    @quote = Quote.new
    @products = Product.all
  end
  
  def create
    @quote = Quote.new(quote_params)
    @products = Product.all
    if @quote.save
      flash[:success] = "Cotización creada correctamente!"
      redirect_to @quote
    else
      @products = Product.all
      render 'new'
    end
  end
  
  def edit
    @quote = Quote.find(params[:id])
    @products = Product.all
  end
  
  def update
    @quote = Quote.find(params[:id])
    @products = Product.all
    if @quote.update_attributes(quote_params)
      flash[:success] = "Cotización actualizada!"
      redirect_to @quote
    else
      @products = Product.all
      render 'edit'
    end
  end
  
  def destroy
    Quote.find(params[:id]).destroy
    flash[:success] = "Cotización eliminada"
    redirect_to quotes_url
  end

  private

    def quote_params
      params.require(:quote).permit(:t_name, :b_name, :maturity, product_ids:[])
    end
end
&#13;
&#13;
&#13;

截至目前,用户可以使用以下格式创建和编辑新报价:

&#13;
&#13;
<% provide(:title, 'Nueva cotización') %>
<h1>Nueva cotización</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@quote, url: new_quote_path) do |f| %>
      <%= render 'shared/error_messages_quotes' %>

      <%= f.label :t_name, "Nombre del tomador" %>
      <%= f.text_field :t_name, class: 'form-control' %>
      
      <%= f.label :b_name, "Nombre del beneficiario" %>
      <%= f.text_field :b_name, class: 'form-control' %>
      
      <%= f.label :maturity, "Año de maduración" %>
      <%= f.text_field :maturity, class: 'form-control' %>
      
      <%= hidden_field_tag "quote[product_ids][]", nil %>
      <% Product.all.each do |product| %>
        <%= check_box_tag "quote[product_ids][]", product.id, @quote.product_ids.include?(product.id) %>
        <%= product.name %>
      <% end %>
      
      <%= f.submit "Crear cotización", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>
&#13;
&#13;
&#13;

报价信息保存在show.html中我能够查看用户在报价中输入的信息。但是,我无法找到如何在价格表中搜索所选的product_ids和给定的成熟年份......

有什么想法吗?

BitBucket链接:https://bitbucket.org/aleanzola/cotizador/src

非常感谢任何帮助!!!

0 个答案:

没有答案