转换rails FormHelper标签的HTML输入标签?

时间:2016-12-15 02:43:53

标签: ruby-on-rails ruby

我对铁轨和编程非常陌生。我有一个rails项目,包括一个带有HTML格式的静态页面,如下所示:

    <span class="form-group">
      <input class="balloon" name="ex1" id="text1" type="text" placeholder="Stuff Here" /><label for="text1">Stuff</label>
    </span>
    <span class="form-group">
      <input class="balloon-num" name="ex2" id="num1" type="number" min=1 max=12 placeholder="1-12" /><label for="num1">Stuff</label>
    </span>
    <span class="form-group">
      <input class="balloon-num" name="ex3" id="num2" type="number" min=1 max=12 placeholder="1-12" /><label for="num2">Stuff</label>
    </span>
    <span class="form-group">
      <input class="balloon-num" name="ex4" id="num3" type="number" min=1 max=5 placeholder="1-5" /><label for="num3">#</label>
    </span>
    <span class="form-group">
      <button class="button" id="generate" type="button"><i class="fi-arrow-right"></i></button>
    </span>

由于CSS中的输入样式,我需要输入类,id和占位符。我现在正在学习如何将这些输入发送到rails中的数据库,据我所知,它需要使用ruby FormHelper语法,如下所示:

<% form_for @example do |f| %>
  <%= f.label :ex1 %>
  <%= f.text_field :ex1 %>
  <%= f.label :ex2 %>
  <%= f.number_field :ex2 %>
  <%= f.label :ex3 %>
  <%= f.number_field :ex3 %>
  <%= f.label :ex4 %>
  <%= f.number_field :ex4 %>
  <%= f.submit 'Submit' %>

我的问题是如何在保留id,name,placeholder等的同时编写后一种格式?

更新:现在获取&#34;表单中的第一个参数不能包含nil或为空&#34;当我检查我的更改是否有效时出错,下面是我的控制器文件:

class SwatchgenController < ApplicationController
  def new
    @colours = Colours.new
  end
  def create
    @colours = Colours.new(colours_params)
    if @contact.save
      redirect_to new_colours_path, notice: "Message sent."
    else
      redirect_to new_colours_path, notice: "Error occured."
    end
  end
  private
  def colours_params
     params.require(:colours).permit(:colour, :bold, :bright, :num)
  end
end

我已经过三重检查,表格标签上写着form_for @colours,所有ID都是正确的。我正在按照用于创建联系页面的教程(也在一个单独的页面上,而我的所有内容都在一个),所以如果这没有意义,这就是原因。

这是我在页面上更新的表格:

<%= form_for @colours do |f| %>
        <%= f.label :colour %>
        <%= f.text_field :content, required: true, class: "balloon", id: "colour", placeholder: "Colour category (red, blue..)" %>
        <%= f.label :bold %>
        <%= f.number_field :content, required: true, class: "balloon-num", id: "bold", placeholder: "1-12" %>
        <%= f.label :bright %>
        <%= f.number_field :content, required: true, class: "balloon-num", id: "bright", placeholder: "1-12" %>
        <%= f.label :num %>
        <%= f.number_field :content, required: true, class: "balloon-num", id: "num", placeholder: "1-5" %>
        <button class="button" id="generate" type="button"><i class="fi-arrow-right"></i></button>
<% end %>

1 个答案:

答案 0 :(得分:2)

您不必使用Rails表单助手。它只是一种构建表单的便捷方式,但如果您想使用自己的表单,则可以这样做。您所要做的就是创建一个表单并使用正确的方法指向正确的操作:

您可以使用纯html表单,这是表单助手所做的事情。您必须在表单中指定要定位的控制器

您可以从form_tag开始,但在里面可以放置普通的HTML表单输入字段

<%= form_tag(send_email_path, method: "post", :class=>"form-horizontal") do %>
  <label class="col-md-3 control-label" for="subject">Subject</label>
  <input id="subject" name="subject" type="text" placeholder="Email Subject" class="form-control">
<% end %>

如果您想使用form_for帮助器,可以将这些内容作为选项传递:

<%= form_for @person do |f| %>
<%= f.text_area :content, required: true, class: "your html class", id: "you html id", placeholder: "your placeholder text" %>

  <%= f.submit %>
<% end %>

修改

您的表单必须在星号所在的位置进行更改:

<%= form_for @colours do |f| %>
        <%= f.label :colour %>
      *  <%= f.text_field :colour, required: true, class: "balloon", id: "colour", placeholder: "Colour category (red, blue..)" %>
        <%= f.label :bold %>
       * <%= f.number_field :bold, required: true, class: "balloon-num", id: "bold", placeholder: "1-12" %>
        <%= f.label :bright %>
       * <%= f.number_field :bright, required: true, class: "balloon-num", id: "bright", placeholder: "1-12" %>
        <%= f.label :num %>
      *  <%= f.number_field :num, required: true, class: "balloon-num", id: "num", placeholder: "1-5" %>
      *  <%= f.submit %>    /// this is your button
<% end %>