2016-05-03 3 views
7

私は次のシナリオを達成しようとしています。基本的に私は2つのモデルRails 4は、Wordpressオプションテーブルと同様の方法でフィールドを保存します

テーマ

  • タイトル

オプション

  • theme_id
を持っています私のフォームで

私は例えば、私のhtml.erbはおそらく次のようになり、ユーザーが設定したオプションとオプションのテーブルを移入できるようにしようとしている:

<%= form_for [:admin, @options],:class => 'form' do |f| %> 

<div class="form-group"> 
<%= f.label :option[:color] %> 
<%= f.text_field :option[:color], :class => 'form-control' %> 
</div> 

<div class="form-group"> 
<%= f.label :option[:header_title] %> 
<%= f.text_field :option[:header_title], :class => 'form-control' %> 
</div> 

<div class="form-group"> 
<%= f.label :option[:footer_copy] %> 
<%= f.text_field :option[:footer_copy], :class => 'form-control' %> 
</div> 

<div class="form-group"> 
<%= f.submit :value => 'Update', :class => 'btn btn-primary' %> 
</div> 

<% end %> 

上記のシナリオは、に3行を追加しますoptions表にはそれぞれ同じtheme_idがあります。

しかし、私はモデルとビュー

任意の助けが理解されるであろう、(取得し、行動を投稿する)コントローラ用に使用する構文がわかりませんよ! おかげ

EDIT 上記のフォームの例が保存されている場合、それは例えばのためのオプションテーブルで3つの別々の行として保存されます:

theme_id |名前|値

1 | "色" | "緑"

1 | "header_title" | 「テーマのタイトルはここに」

1 | "footer_copy" | "lorem ipsum bla bla"

答えて

2

はあなたのビュー/テーマ/ _formに

class ThemesController < ApplicationController 
    def new 
    @theme = Theme.new 
    3.times { @theme.options.build } 
    end 

private 
    def theme_params 
    params.require(:theme).permit(:title, options_attributes: [:id, :name, :value, :_destroy]) 
    end 
end 

class Option < ActiveRecord::Base 
    belongs_to :theme 
end 

class Theme < ActiveRecord::Base 
    has_many :options 

    accepts_nested_attributes_for :options, allow_destroy: true 
end 


クライアント側で動的にこれらのフィールドを生成する必要があります。html.erb application_helper.rbアドオンで

$(document).on "ready page:load", -> 
    $('form').on 'click', '.remove_fields', (event) -> 
    $(this).prev('input[type=hidden]').val('1') 
    $(this).closest('fieldset').hide() 
    event.preventDefault() 

    $('form').on 'click', '.add_fields', (event) -> 
    time = new Date().getTime() 
    regexp = new RegExp($(this).data('id'), 'g') 
    $(this).before($(this).data('fields').replace(regexp, time)) 
    event.preventDefault() 

を追加するには、themes.coffeeファイルに新しいパーシャルビュー/テーマ/ _option_fields.html.erb

<fieldset> 
    <%= f.select :name, [["Color", "color"], ["Header", "header_title"], ["Footer", "footer_copy"]] %> 
    <%= f.text_field :value, placeholder: "Value" %> 
    <%= f.hidden_field :_destroy %> 

    <%= link_to "remove", '#', class: "remove_fields" %> 
</fieldset> 

を作成

<%= form_for(@theme) do |f| %> 
    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 

    <%= f.fields_for :options do |builder| %> 
    <%= render 'option_fields', f: builder %> 
    <% end %> 

    <%= link_to_add_fields "Add", f, :options %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

を追加

module ApplicationHelper 
    def link_to_add_fields(name, f, association) 
    new_object = f.object.send(association).klass.new 
    id = new_object.object_id 
    fields = f.fields_for(association, new_object, child_index: id) do |builder| 
     render(association.to_s.singularize + "_fields", f: builder) 
    end 
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) 
    end 
end 

結果

enter image description here

1

このようなプロジェクトを作成したとき、OptionsにはCRUD機能を持つコントローラがありました。

ビデオモデル:

theme.rb 

has_many :options 

オプションモデル

option.rb 
belongs_to :video 

オプションコントローラー:

def create 
@option = Option.new(option_params) 
@video.theme_id = @theme.id 
if @option.save 
    ... 
end 

希望はこれが便利です。

+0

ありがとうございました。これはオプションテーブルへの単一のエントリのためにあるようですか?私は1つのフォームがオプションテーブルに複数のエントリを追加できる何かを探しています – uguMark

+0

私はそれを読んで、色、ヘッダタイトル、およびフッタをフィールドとして持つオプションモデルを持つように見えました。あなたはこれらのアイテムのすべてを分離した状態にしていますか? –

+0

ええと、optionsテーブルには、theme_id、name、valueというフィールドしかありません。元の質問を編集して、Optionsテーブルに保存する方法の例を挙げました – uguMark

1

次のようにあなたは、テーマリソース、コントローラとビューでfields_for使用することができます。

# routes.rb 
resources :themes 

# app/controllers/themes_controller.rb 
def new 
    @theme = Theme.new 
end 

def create 
@theme = Theme.new theme_params 
@theme.save! 

[:color, :header_title, :footer_copy].each do |option_name| 
    theme.options.create! name: option_name, value: params[:theme][option_name] 
end 
end 

private 
def theme_params 
    params.require(:theme).permit(:title) 
end 

#app/views/themes/new.html.erb 
<%= form_for @theme do |f| %> 
    <div> 
    <%= f.label :title %> 
    <%= f.text_field :title %> 
    </div> 

    <% [:color, :header_title, :footer_copy].each do |option_name| 
    <%= f.fields_for option_name do |option_form| %> 
     <div> 
     <%= option_form.label option_name %> 
     <%= option_form.text_field option_name %> 
     </div> 
    <% end %> 
    <% end %> 
<% end %> 

必要に応じては、上記になど、エラーチェック、スタイリングを、追加することができます。団体やaccepts_nested_attributes_forに建て

1

使用レール:

class Theme < ActiveRecord::Base 
    has_many :options 
    accepts_nested_attributes_for :options, reject_if: :all_blank, allow_destroy: true 
end 

class Option < ActiveRecord::Base 
    belongs_to :theme 
end 

、すべてのJSコードを書くことを避けるためにcocoon宝石を使用しHAMLの例が、あなたはポイントを取得する必要があります:

application.js

//= require cocoon 

フォーム:

= form_for @theme do |f| 
    .field 
    = f.label :title 
    %br 
    = f.text_field :title 
    %h3 Options 
    #option 
    = f.fields_for :options do |option| 
     = render 'options_fields', f: option 
    .links 
     = link_to_add_association 'add option', f, :options 
    = f.submit 

_options_fields

.nested-fields 
    .field 
    = f.label :name 
    %br 
    = f.text_field :name 
    .field 
    = f.label :value 
    %br 
    = f.text_field :value 
    = link_to_remove_association "remove option", f 

質問

あなたは、nフィールド(オプション)、または単にこれらの3をしたいですか?コクーンはnオプションを追加するのに適しています。

関連する問題