0

私のRailsアプリケーションはアカウントに基づいています。したがって、すべてのユーザーはアカウント、すべてのプロジェクトなどに属します。

現在、次のようなルートがあります。

/mission-control
/tasks
/projects

そして、私はユーザーによって現在のアカウントを取得しています。ユーザーは多くのアカウントへのアクセス許可を持つことができるはずなので、次のルートが必要です。

/:account_id/mission-control
/:account_id/tasks
/:account_id/projects

私は書くことができることを知っています:

resource :accounts do
  resource :tasks
end

しかし、これは例えば

/accounts/1/tasks

誰かがそのためのルートを書く方法を私に助けてくれることを願っています!

4

2 に答える 2

2

今、私は正しい方法を手に入れました:

最初は、次のようなスコープを定義する必要がありました。

scope ":account_id" do
  resources :tasks
  resources :projects
end

次に、すべてを機能させるには、次のようなループ内にリンクを作成します。

<%= link_to "Project", project %>

動作しません。アプリケーションコントローラでデフォルトのURLオプションを設定する必要があります。

def default_url_options(options={})
  if @current_account.present?
    { :account_id => @current_account.id }
  else
    { :account_id => nil }
  end
end

それは私のためにすべてを修正No Route Matches Errorします。:account_idがない場合、たとえばそのデバイスの場合など、エラーは発生しません。

@Mohamadの場合:

before_filter :set_current_account  

# current account
def set_current_account
  # get account by scoped :account_id

  if params[:account_id]
    @current_account = Account.find(params[:account_id])
    return @current_account
  end

  # dont' raise the exception if we are in that devise stuff
  if !devise_controller?
    raise "Account not found."
  end
end

その工夫とエラー処理はより良いかもしれません。:S

于 2013-02-24T09:29:38.880 に答える
1

次のようなスコープを実行できます。

scope ":account_id" do
  resources :tasks
  resources :projects
end
于 2013-02-13T19:32:37.330 に答える