RAILS - 在提交表单时添加消息

时间:2013-09-11 17:23:16

标签: ruby-on-rails ruby ruby-on-rails-3 forms

我似乎无法解决这个问题或在任何地方找到解决方案......这对我来说很疯狂,因为我觉得它非常普通和简单

我想添加一条消息,当客户通过表单向客户发送请求时,我的客户会看到该消息,然后转到外部API,他们可以看到创建的票证。

所以现在我的客户看到了

 John Doe 

但我希望他们看到

 Web inquiry from John Doe

所以我需要通过表格

发送“网页查询”部分

我试图以

的形式插入它
 = f.text_field "Web inquiry from #{:subject}"

没有用

我试图添加一个值(不是我想去的方式,但无论如何我都试过了)

 = f.text_field :subject, value: "Web inquiry from #{f.object.subject}"

既不起作用

我试图把它放在模型中

 def post_tickets(params)
   client.subject = "Hello from, " + client.subject
 end

我是rails的新手,所以如果你能尽可能具体,那将会有所帮助......请不要说只是在控制器中做到这一点......谢谢先进

这是我的表格

= form_for(:contacts, url: contacts_path) do |f|
= f.error_messages
= f.label :subject, "Name"
%span{style: 'color: red'} *
= f.text_field :subject, class: "text_field width_100_percent"
%br
%br    
= f.label "Email"
%span{style: 'color: red'} *
%br    
= f.email_field :email, class: "text_field width_100_percent"
%br
%br
= f.label "Question(s), and/or feedback"
%span{style: 'color: red'} *
%br
= f.text_area :description, class: "text_field width_100_percent", style: 'height: 100px;'
%br
%br
= f.submit "Submit", class: 'btn btn-warning'

这是我的控制器

 class Website::ContactsController < Website::WebsiteApplicationController
   def new
     @contacts = Form.new
   end

   def create
     @contacts = Form.new(params[:contacts])
     @contacts.post_tickets(params[:contacts])
     if @contacts.valid?
       flash[:success] = "Message sent! Thank you for conacting us."
       redirect_to new_contact_path
     else
       flash[:alert] = "Please fill in the required fields"
       render action: 'new'
     end
   end
 end

这是我的模特

 class Form
   include ActiveModel::Validations
   include ActiveModel::Conversion
   include ActiveModel::Translation
   extend  ActiveModel::Naming

   attr_accessor :config, :client, :subject, :email, :custom_field_phone_number_28445, 
            :custom_field_name_28445, :custom_field_company_28445, :description, 
            :custom_field

    validates_presence_of :subject, :message => '^Please enter your name'
    validates_presence_of :description, :message => '^Question(s), and/or feedback can not be blank'
    validates :email, presence: true  
    validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i

    def initialize(attributes = {})
      attributes.each do |name, value|
        @attributes = attributes
      end

      self.config = YAML.load_file("#{Rails.root}/config/fresh_desk.yml")[Rails.env]
  self.client = Freshdesk.new(config[:url], config[:api_key], config[:password])
    end

    def read_attribute_for_validation(key)
      @attributes[key]
    end

    def post_tickets(params)
      client.post_tickets(params)
    end

    def persisted?
      false
    end
  end

1 个答案:

答案 0 :(得分:1)

def post_tickets(params)
  # prepend to the params['subject'] just before posting
  client.post_tickets "Web enquiry from #{params['subject']}"
end