2016-12-15 11 views
1

私は一般的なレールとプログラミングにはとても新しいです。次のように私は、HTMLのフォームと静的ページを含むレールのプロジェクトを持っている:FormHelperタグの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をプレースホルダを必要としています。そのように、私は今、レール内のデータベースにこれらの入力を送信する方法を学んでいる、と私の理解には、ルビー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、名前、プレースホルダーなどを保持していますか?


UPDATE:私は私の変更は以下の、働いていたかどうかを確認するとき今すぐ「フォームの最初の引数が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 

I 3回チェックし、フォームタグにはform_for @coloursと表示され、すべてのIDは正しいものです。私はコンタクトページを作成するためのチュートリアルに従っています(別のページにも、私のものはすべて1つです)。

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

シンプルフォームと呼ばれる便利な宝石があります:https://github.com/plataformatec/simple_form – Brad

答えて

2

あなたはRailsのフォームヘルパーを使用する必要はありません。ここで

は、ページ上の私の更新の形です。フォームを作成する便利な方法ですが、独自のフォームを使用したい場合は、そのフォームを使用できます。フォームを作成し、正しい方法で正しいアクションを指すようにするだけです。

フォームヘルパーがとにかく実行する純粋なhtmlフォームを使用できます。あなたは、あなたの形でターゲットにされているコントローラを指定する必要があります

あなたはform_tagで始まるが、あなたが使用したい場合は、

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

通常のHTMLフォームの入力フィールドに置くことができます内側ができ

あなたはオプションとしてそれらのものを渡すことができますの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 %> 

EDIT

フォームはアスタリスクがどこにあるか変更する必要があります。

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

ありがとうございます、私は今、エラーが発生しています: "フォームの最初の引数は、nilを含めることができないか空ではありません"ページが正常に動作しているかどうかを確認し、理由を確認します。私はコントローラファイルで最初の質問を更新しました。 – Lacomus

+1

フォームを作成するときに、インスタンス変数を渡すことはできますが、そのインスタンス変数はコントローラで定義する必要があります。たとえば、show.html.erbのform_for "@example"は、コントローラのshowアクションで "@example"を定義しない限り、エラーを表示します。そうすれば、あなたが "@example"に投稿するとき、あなたのコントローラは何をすべきか知っています。 –

+0

私はそうではありません: def new @colours = Colours.new 私のコントローラファイルの末尾に? – Lacomus

関連する問題