【问题标题】:Rails - adding a string in front of a form parameterRails - 在表单参数前添加字符串
【发布时间】:2013-09-14 16:53:36
【问题描述】:

我很迷茫,似乎无法在任何地方找到解决方案...

我想要做的就是当他们通过外部 API 提交表单时,能够在我的用户名前添加一点问候语......所以没有任何内容被保存到我的数据库中

我想做的就是这个

 "Web inquiry from Jon Doe"

所以我有一个表格

  <%= text_field :subject %>

当用户输入他的名字并提交表单时,我的客户可以转到发布表单的外部网站并查看

 "Web inquiry from Jon Doe"

所以我只需要能够坚持下去

 Web inquiry from 

表格上的名字的一部分?

请帮助我...你唯一的希望

这是我的控制器

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

这是我的表格

 = 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 "Phone"
    %br    
    = f.text_field :custom_field_phone_number_28445, name: 'contacts[custom_field][phone_number_28445]', class: "text_field width_100_percent"
    %br
    %br
    = f.label "Company"
    %br    
    = f.text_field :custom_field_company_28445, name: 'contacts[custom_field][company_28445]', 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'

【问题讨论】:

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


    【解决方案1】:

    这还不够吗?

    message = "Web inquiry from " + params[:subject]
    

    【讨论】:

    • 对不起,我刚接触 Rails....我将如何使用它?我会把它放在控制器还是模型中?
    • 可能在显示它的视图中。
    • = f.text_field :subject, message = "来自" + params[:subject] 的网络查询.....这样吗?
    • 我得到一个无法将 nil 转换为字符串
    • 是否要预填充“来自的 Web 查询”部分?还是您在事后附加?您的问题表明它稍后会在显示时添加,在这种情况下,它会进入呈现此记录的关联视图,而不是您首先创建它的表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多