我有一个客户端资源有两种类型:人员和公司.
routes.rb中:
resources :clients
resources :people, :controller => "clients", :type => "Person"
resources :companies, :controller => "clients", :type => "Company"
Run Code Online (Sandbox Code Playgroud)
clients_controller:
def new
@client = Client.new()
@client.type = params[:type]
end
def create
@client = current_partner.clients.new(client_params)
if @client.save
redirect_to clients_path
...
end
...
private
def client_params
params.require(:client).permit(:type, :partner_id, :name, :email, :phone, :cui, :registration_id, :address)
end
def find_client
@client ||= Client.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
client.rb
class Client < ActiveRecord::Base
validates_presence_of :type
CLIENT_TYPES = ['Person', 'Company']
end
Run Code Online (Sandbox Code Playgroud)
person.rb
class Person < Client
validates_presence_of :name, :email, …Run Code Online (Sandbox Code Playgroud) 如何将HTML视图呈现为字符串(保存在数据库中)以便能够在rails中将其呈现回来?
def show_offer
respond_to do |format|
format.html { render 'offer_template_one', :layout => 'templates'}
format.pdf do
render :pdf => 'oferta',
:template => 'templates/show_offer.pdf.erb',
:layout => "layouts/templates.html.erb",
:save_to_file => Rails.root.join('public', "Oferta.pdf")
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我渲染视图的方法
谢谢!
我有一个状态报告,用户可以通过电子邮件发送该报告,我想在完成传递操作后将:sent_mail列更新为true。
def send_status
date = Date.today
reports = current_user.reports.for_date(date)
ReportMailer.status_email(current_user, reports, date).deliver
reports.update_all(sent_mail: true)
end
Run Code Online (Sandbox Code Playgroud)
和表类
AddSentMailToReports < ActiveRecord::Migration
def change
add_column :reports, :sent_mail, :boolean, default: false
end
end
Run Code Online (Sandbox Code Playgroud)
但是,在控制台中,send_mail仍然设置为false。有什么想法为什么不起作用?谢谢!
假设我有一个"城市"或"国家/地区"输入字段.如何验证此字段仅包含字母?
目前我用这样的空格替换任何最终的数字字符:
def strip_numeric_characters
self.city = self.city.gsub(/[0-9]/, "")
self.country = self.country.gsub(/[0-9]/, "")
end
Run Code Online (Sandbox Code Playgroud)
但这不是正确的方法,因为如果有人只输入数字,我最终将使用空字符串.