0

Ruby on rails の初心者として、アプリの一部に出くわしました。RoR フレームワークの基本を読み、Rails MVC の「設定より規約」機能を理解しています。2 つのテーブルがあり、1 つはapps_eventsで、もう 1 つはapps_events_attributesです。最初のIDは2番目の外部キーであり、多くの関係があります。app_events テーブルには外部キー属性「app_id」のフィールドがあるため、特定のアプリを選択すると、そのイベントと属性にリダイレクトされます。「is_standard」と呼ばれるフィールドもあり、イベント タイプが標準イベントかカスタムイベントかを実際に区別します。

次に、nested_form_for 機能を使用して、特定のアプリのこれらのイベントとその属性をビュー レイヤーの 2 つの異なるタブにレンダリングする必要があります。ユーザーは、このタブをクリックして標準イベントとカスタム イベントを切り替えることができます。誰かが私に同じことを達成する方法を教えてもらえますか?このシナリオの理想的なフローを教えてもらえますか?

ところで、同じモデルで別のコントローラーを使用できますか? 同じようにすると、別のイベントとその属性に対して同じ CRUD 機能を実行できますか?

4

1 に答える 1

0

私はすべてを一人でやり遂げましたが、最初はそれほど難しくないと思います。すべてのトリックはJQueryによって行われます...誰かが私と同じ問題を抱えている場合は、その概念を共有してください

私のモデル

class AppsEvent < ActiveRecord::Base
  belongs_to :app
  has_many :apps_events_attributes, :foreign_key => 'apps_event_id',
       :class_name => 'AppsEventsAttribute',
       :dependent => :destroy
  accepts_nested_attributes_for :apps_events_attributes,
                            :reject_if => lambda { |a| (a[:name].blank? &&    a[:description].blank?) },
                            :allow_destroy => true
  validates_presence_of :description
  validates_presence_of :name
  validates_presence_of :code
  validates_presence_of :apps_events_attributes, :message => "can't be empty"
end  

class AppsEventsAttribute < ActiveRecord::Base
  set_table_name :apps_events_attributes
  belongs_to :apps_event, :foreign_key => 'apps_event_id'
  attr_accessible :id, :apps_event_id, :name, :description, :attribute_type, :is_std, :created_at, :updated_at

  def type
    self.attribute_type
  end
end  

私のコントローラーは...

class AppsEventsController < ApplicationController
  layout :layout_by_resource
  before_filter :initialize_default_app
  before_filter :check_permission
  before_filter :load

  def load
    @app = current_user.find_app(params[:app_id])
    @apps_events = AppsEvent.where(:app_id => @app.id)
    @apps_event = AppsEvent.new
  end

  def index
    @app = current_user.find_app(params[:app_id])
    @default_app = App.default(current_user)
    @apps_events = AppsEvent.where(:app_id => @app.id)
    @apps_event_jsons = Hash.new
    @apps_events.each do |app_event|
      json = Hash.new
      json['User_ID'] = 548741213
      json['Session_ID'] = 2568639390
      json['Action_Type'] = app_event.code
      json['eventsData'] = {}

      app_event.apps_events_attributes.each do |apps_event_attributes|
        if (apps_event_attributes.attribute_type == 'Integer')
          json['eventsData'][apps_event_attributes.name] = 1234
        elsif (apps_event_attributes.attribute_type == 'Float')
          json['eventsData'][apps_event_attributes.name] = 1234.23
        else
          json['eventsData'][apps_event_attributes.name] = 'abcd'
        end
      end

      @apps_event_jsons[app_event.id] = json
    end
  end

  def new
    @apps_event = AppsEvent.new
    @app = current_user.find_app(params[:app_id])
    @apps_event.app_id = @app.id
    @apps_event.apps_events_attributes.build
    @action = 'create'
    render 'edit'
  end

  def edit
    @app = current_user.find_app(params[:app_id])
    @apps_event = AppsEvent.find(params[:id])
    @action = 'update'
  end

  def create
    @apps_event = AppsEvent.new(params[:apps_event])
    @show_custom_event = 'true'
    @apps_event.name = @apps_event.name.strip
    respond_to do |format|
      if @apps_event.save
        format.html {
          redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                  :notice => "Successfully created #{@apps_event.name} custom definition.")
        }
        format.js {
          flash[:notice] = 'Successfully created event.'
          @apps_events = AppsEvent.where(:app_id => @app.id)
        }
      else
        @app = current_user.find_app(params[:app_id])

        if (@apps_event.apps_events_attributes == nil ||   @apps_event.apps_events_attributes.size <= 0)
          @apps_event.apps_events_attributes.build
        end

        @apps_event.populate_code
        @action = 'create'
        format.html {
          redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                  :alert => "Error in creating #{@apps_event.name} custom definition.")
        }
        format.js
      end
    end
  end

  def update
    @apps_event = AppsEvent.find(params[:apps_event][:id])
    params[:apps_event][:name] = params[:apps_event][:name].strip

    respond_to do |format|
      if @apps_event.update_attributes(params[:apps_event])
        format.html {
          if(@apps_event.is_std == 'y')
            redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                  :notice => "Successfully updated #{@apps_event.name} standard definition.")
          else
            redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                    :notice => "Successfully updated #{@apps_event.name} custom definition.")
          end
        }
        format.js {
          flash[:notice] = 'Successfully updated event.'
          @apps_events = AppsEvent.where(:app_id => @app.id)
          render :nothing => true
        }
      else
        @app = current_user.find_app(params[:app_id])

        if (@apps_event.apps_events_attributes == nil || @apps_event.apps_events_attributes.size <= 0)
          @apps_event.apps_events_attributes.build
        end

        @apps_event.populate_code
        @action = "update"
        format.html {
          if(@apps_event.is_std == 'y')
            redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                  :alert => "Error in updating #{@apps_event.name} standard definition.")
          else
            redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
                    :alert => "Error in updating #{@apps_event.name} custom definition.")
          end
        }
        format.js
      end
    end
  end

  def delete
    if AppsEvent.delete(params[:id])
      redirect_to "/app/#{params[:app_id]}/apps_events", :notice => "Successfully deleted #{@apps_event.name} custom definition."
    else
      redirect_to "/app/#{params[:app_id]}/apps_events",
              :alert => "Error in deleting #{@apps_event.name} custom definition."
    end
  end
end  

そして、index.html.erb、edit.js.erb、_form_custom.html.erb、_form_standard.html.erb、および_events.html.erbの5つのビューファイルがあり、その横に更新、作成、および削除用のヘルパーファイルもありますremote => true を設定して ajax 呼び出しを使用します。インデックスファイルでは、すべてのイベント(_events.html.erb)を部分的にレンダリングしていますが、ここでトリックを行いました:P
My _events.html.erb

<% for apps_event in @apps_events %>
  <% if (apps_event.is_std == 'y') %>
    <div class="standardEvent showStandard">
      <ul>
        <li class="column_1"><span style="font-weight: bold;"><%= apps_event.name %></span></li>
        <li class="column_2"><span><%= apps_event.code %></span></li>
        <li class="column_3"><span><%= apps_event.description %></span></li>
        <li class="column_4">
          <%= link_to edit_apps_event(apps_event) %>
        </li>
        <li class="column_5">

        </li>
      </ul>
    </div>
  <% else %>
    <div class="customEvent showCustom" style="display:none">
      <ul>
        <li class="column_1"><span style="font-weight: bold;"><%= apps_event.name %></span></li>
        <li class="column_2"><span><%= apps_event.code %></span></li>
        <li class="column_3"><span><%= apps_event.description %></span></li>
        <li class="column_4">
          <%= link_to edit_apps_event(apps_event) %>
        </li>
        <li class="column_5">
          <%= remove_apps_event_prompt_link(apps_event) %>
        </li>
      </ul>
    </div>
  <% end %>
  <div class="clrBoth"></div>
<% end %>

これで、左側の部分の意味を理解できます-divを非表示または表示するJQuery部分。

于 2013-07-09T11:59:18.230 に答える