Ruby on rails 在窗体的参数前面添加字符串

Ruby on rails 在窗体的参数前面添加字符串,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我想在表单上的参数前面添加一个字符串,这样当用户提交表单上的内容时,它会发布到外部API,我的客户端可以登录到freshdesk.com,而不是说BOB,而是说Hello from BOB Hello from [:username] 在我看来,我试过这样做: = f.text_field "Hello From, #{:username}" 但它不起作用。我还尝试使用一个值: = f.text_field :subject, value: "Hello From, #{:username

我想在表单上的参数前面添加一个字符串,这样当用户提交表单上的内容时,它会发布到外部API,我的客户端可以登录到freshdesk.com,而不是说
BOB
,而是说
Hello from BOB

Hello from [:username]
在我看来,我试过这样做:

= f.text_field "Hello From, #{:username}" 
但它不起作用。我还尝试使用一个值:

= f.text_field :subject, value: "Hello From, #{:username}"
但这也不起作用。这是我的表格:

= 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'
这是我的控制器:

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
这是我的模型

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

您应该在模型中这样做,在表单发送值之前添加子字符串。这似乎是业务逻辑,不应该出现在视图中

def post_tickets(params)
   client.username = "Hello From, " + client.username
   client.post_tickets(params)
end

您应该在模型中这样做,在表单发送值之前添加子字符串。这似乎是业务逻辑,不应该出现在视图中

def post_tickets(params)
   client.username = "Hello From, " + client.username
   client.post_tickets(params)
end

您的视图应该具有没有魔力的简单字段。我们将使用Form类来完成复杂的工作

= f.text_field :subject
post_tickets的方法调用不需要接收
params
,因为表单对象已经用
params
值初始化。还有,我想你不应该把票寄出去,除非票上的东西是有效的,对吗

def create
  @contacts = Form.new(params[:contacts])
  if @contacts.valid?
    @contacts.post_tickets
    flash[:success] = "Message sent! Thank you for contacting us."
    redirect_to new_contact_path
  else
    flash[:alert] = "Please fill in the required fields"
    render action: 'new'
  end
end
表单模型应负责修改:subject参数以包含前缀:

class Form
  # Some code omitted

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

  def post_tickets
    client.post_tickets({
      :whatever_fields => whatever_attribute, 
      :username => username, 
      :subject => "Hello from, #{subject}", 
      :email => email, 
      :description => description
    })
  end

end
这样,表单对象具有用户提交的正确值,但您已经覆盖了发布的主题,因此它将返回所需的组合字符串,并带有“Hello from…”

在post_票证中,通过初始化表单对象的属性直接引用要发送的参数。对于
主题
,您将发送一个组合值

请注意,我已经重写了您的初始化,以使用更经典的属性设置方法


想法,问题

您的视图应该具有没有魔力的简单字段。我们将使用Form类来完成复杂的工作

= f.text_field :subject
post_tickets的方法调用不需要接收
params
,因为表单对象已经用
params
值初始化。还有,我想你不应该把票寄出去,除非票上的东西是有效的,对吗

def create
  @contacts = Form.new(params[:contacts])
  if @contacts.valid?
    @contacts.post_tickets
    flash[:success] = "Message sent! Thank you for contacting us."
    redirect_to new_contact_path
  else
    flash[:alert] = "Please fill in the required fields"
    render action: 'new'
  end
end
表单模型应负责修改:subject参数以包含前缀:

class Form
  # Some code omitted

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

  def post_tickets
    client.post_tickets({
      :whatever_fields => whatever_attribute, 
      :username => username, 
      :subject => "Hello from, #{subject}", 
      :email => email, 
      :description => description
    })
  end

end
这样,表单对象具有用户提交的正确值,但您已经覆盖了发布的主题,因此它将返回所需的组合字符串,并带有“Hello from…”

在post_票证中,通过初始化表单对象的属性直接引用要发送的参数。对于
主题
,您将发送一个组合值

请注意,我已经重写了您的初始化,以使用更经典的属性设置方法



想法,问题

它实际上不是一封电子邮件…它是一个提交给另一个网站API的表单…它叫FRESHDESK.com那么我想你有一个动作某个控制器收到了这个表单,不是吗?您可以从表单中获取参数并在调用这些web服务之前附加子字符串。我发布了我的controllerI,我认为我应该在模型中创建一个新方法“welcome”,并在您要调用API时从controller中调用它。这个方法只需要返回“Hello From,#{username}”。dam有没有一个简单的解决方案…我也找不到关于这个的任何其他讨论…它实际上不是一封电子邮件…它是一个提交到另一个网站API的表单…它叫做FRESHDESK.com。那么我想你有一个操作某个控制器接收这个表单,不是吗?您可以从表单中获取参数并在调用这些web服务之前附加子字符串。我发布了我的controllerI,我认为我应该在模型中创建一个新方法“welcome”,并在您要调用API时从controller中调用它。这个方法只需要返回“Hello From,#{username}”。这个问题没有简单的解决方案……我也找不到关于这个问题的任何其他讨论。你是否尝试过将(@contacts)的表单改为
表单_,并将插值改为
“Hello From,#{f.object.username}”
@rubish-是的,我试过了,但没用……当我把它改成@contacts时,我得到了一个未定义的方法'each'来表示nil:nilclass试着把
@contacts
变量名改成
@contact
。Rails是非常传统的。@rubish-当我去掉联系人末尾的s时,我得到了一个未定义的NilClass方法“model\u name”:ClassCan you post your model。您要么坚持rails约定(此处为命名约定)以获得轻松生活,要么偏离约定并面临需要解决的问题。这是您的选择,但我建议您坚持约定。您是否尝试过将(@contacts)
的表单更改为
form_,并将插值更改为
“Hello From,#{f.object.username}”
@rubish-是的,我试过了,但没用……当我把它改成@contacts时,我得到了一个未定义的方法'each'来表示nil:nilclass试着把
@contacts
变量名改成
@contact
。Rails是非常传统的。@rubish-当我去掉联系人末尾的s时,我得到了一个未定义的NilClass方法“model\u name”:ClassCan you post your model。您要么坚持rails约定(此处为命名约定)以获得轻松生活,要么偏离约定并面临需要解决的问题。这是您的选择,但我建议您遵守约定。感谢您提供的详细答案…但是我得到了一个未定义的方法“read_attribute”,这是因为我没有使用active record吗?我已更新了我的答案,以删除覆盖的读取访问器,而只是,在传递给
client.post\u tickets
的值中发送了修改后的字符串。是否仍正确设置了
self.config
self.client
?表单类对象上的属性设置是否正确?你过去常干什么