【问题标题】:RAILS - tacking on a message when form is submitted [duplicate]RAILS - 提交表单时添加消息[重复]
【发布时间】:2013-09-15 20:25:34
【问题描述】:

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

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

所以现在我的客户看到了

 John Doe 

但我想让他们看到

 Web inquiry from John Doe

所以我需要通过表单发送“Web 查询来自”部分

我尝试将其插入到表单中

 = 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

【问题讨论】:

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


    【解决方案1】:
    def post_tickets(params)
      # prepend to the params['subject'] just before posting
      client.post_tickets "Web enquiry from #{params['subject']}"
    end
    

    【讨论】:

    • 我得到一个无法将 nil 转换为字符串
    • 我确实有它的主题
    • 我已经修改了post_tickets 方法以包含这些措辞。看看有没有效果
    • 您不应该在不检查 var 是否为 nil? 的情况下将 + var 转换为字符串。使用插值
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    相关资源
    最近更新 更多