Ruby on rails 将HTML输入标记转换为rails FormHelper标记?
一般来说,我对rails和编程非常陌生。我有一个rails项目,包括一个静态页面,其HTML格式如下:Ruby on rails 将HTML输入标记转换为rails FormHelper标记?,ruby-on-rails,ruby,Ruby On Rails,Ruby,一般来说,我对rails和编程非常陌生。我有一个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" 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。我现在正在学习如何将这些输入发送到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、名称、占位符等
更新:当我检查更改是否有效时,发现“表单中的第一个参数不能包含nil或为空”错误,下面是我的控制器文件:
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
class-SwatchgenController
我已经检查了三遍,表单标签上写着@colors的form_,所有的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 %>
您不必使用Rails表单助手。这只是一个方便的方式建立形式,但如果你喜欢使用自己的形式,你可以这样做。您所要做的就是创建一个表单,并使用正确的方法指向正确的操作: 您可以使用纯html表单,这也是表单帮助器所做的。您必须在表单中指定目标控制器 您可以从form_标记开始,但可以将普通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 %>
“表单水平”)do%>
主题
如果您想使用表单_for helper,可以将这些内容作为选项传递:
<%= 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 %>
*
*
*
*
*///这是你的按钮
谢谢,我现在遇到一个错误:“表单中的第一个参数不能包含nil或为空”,当我试图查看页面以查看它是否有效时,不确定原因。我已经用我的控制器文件更新了最初的问题。当你创建一个表单时,你可以像以前一样向它传递一个实例变量,但是你必须在你的控制器中定义这个实例变量。例如,show.html.erb中“@example”的表单_将呈现错误,除非在控制器的show操作中定义“@example”。这样,当你发布到“@example”时,你的控制器知道该做什么。我不是在我的控制器文件中使用:def new@colors=colors.new end吗?是的,要消除参数错误,你需要在控制器中定义该实例变量。你不必创建实例变量。如果愿意,可以发布到路径,然后可以使用params[:content]表示法获取表单的输入。例如,假设“@example”是post,它有自己的控制器,具有索引、显示、创建、更新、编辑和删除操作。您可以直接发布到路径,而不是像这样使用instace变量:form_例如将发布到示例控制器的路径。当您的控制器收到POST请求时,它会知道您想要创建一个对象,因为POST是为创建对象保留的HTTP谓词。好的,我可以建议您运行一个单独的项目,使用scaffold创建一个控制器,与您的直接项目进行比较,看看您可能缺少什么。你也可以这样学习。不过,请阅读Rails指南。有一个名为simple_form的gem,您可能会发现它很有用: