2016-12-15 141 views
1

我对rails和编程一般都很陌生。我有一个Rails项目,包括在HTML表单如下静态页面:将HTML输入标签转换为FormHelper标签?

<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> 

我需要输入类,ID和占位由于CSS输入的造型。现在我正在学习如何在铁轨这些输入发送到数据库,并以我的理解它需要使用红宝石表单助手语法,就像这样:

<% 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,名称,占位符等?


UPDATE:现在越来越“在形式上第一个参数不能包含零或为空”的错误,当我检查,看看我的变化工作,下面是我的控制器文件:

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 %> 
+0

好吧,我可以告诉你运行一个单独的项目,创建具有支架控制器,进行比较同直接的项目,看看你是什么可能会失踪。你也可以这样学习。但请阅读Rails指南。 –

+0

有一个名为simple_form的gem,你可能会觉得很有用:https://github.com/plataformatec/simple_form – Brad

回答

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 %> 
+0

谢谢,我现在收到一个错误:当我尝试“表单中的第一个参数不能包含零或为空”时查看页面以查看它是否工作,不知道为什么。我用我的控制器文件更新了最初的问题。 – Lacomus

+1

当你创建一个表单时,你可以像你一样将它传递给一个实例变量,但是你必须在你的控制器中定义该实例变量。例如,除非在控制器的show动作中定义“@example”,否则show.html.erb中的“@example”将显示一个错误。这样,当你发布到“@example”时,你的控制器知道该怎么做。 –

+0

是不是我在做: def new @colours = Colours.new 在我的控制器文件中结束? – Lacomus