2016-07-14 11 views
0

はここに私の現在の状況です。ある企業は、第3のモデル:moderator_connectionsによって多くのモデレートユーザーを持つことができ、その逆もあります。現在のモデレーターを選択するには、current_company(参照されていない)列をモデル:usersに追加しました。他のコントローラのフォームを使用して、Rails4でhas_many:throughリレーションを持つ値を設定する方法は?私はモデル<code>:companies</code>とモデル<code>:users</code>(<code>Devise</code>)を持っている</p> <p>:

ウェブサイトのメインメニューにdropdown_menuを追加します。このメニューから現在の会社を選択すると、ユーザーはその会社のコンテンツに切り替えることができます。私はこれを、application.html.erb<%= yield %>の上にフォーム部分を描画することによって試みました。 formここ

current_userのために(接続された企業の一つのcompany_idにそれを変更することによって)current_company整数を編集しようとしますが、私はそれを解決するためにも近接していないだと思う:(

は私の関係です。

/models/company.rb:

has_many :moderator_connections  
has_many :moderators, through: :moderator_connections, class_name: 'User', foreign_key: 'company_id' 

/models/user.rb:

has_many :moderator_connections 
has_many :moderated_companies, through: :moderator_connections, class_name: 'Company', foreign_key: 'user_id' 

/モデル/ MOD erator_connection.rb:

belongs_to :user 
belongs_to :company 

/layouts/application.html.erb:

<main> 
    <div class="container"> 
     <%= render partial: "users/current_company_form" %> 

     <%= render partial: "shared/message" %> 

     <%= yield %> 
    </div> 
</main> 

が/users/_current_company_form.html.erb:

<% if user_signed_in? %> 
<%= form_for edit_user_registration_path do |f| %> 
    <div class="form-group"> 
     <%= f.collection_select :current_company, current_user.moderator_connections(:company_id), :name, :id,{ prompt: "Choose a company" } %> 

     <%= f.hidden_field :current_company, :value => current_user.moderator_connections(:company_id) %> 

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

それはすでにとうまくいきません部分のレンダリング、次のエラーが表示されます(私が入っているページによって異なります):No route matches [POST] "/pages/welcome"

正直言って、私はこれをどのように作成するのか分かりません(私の愚かな試みから結論づけることができます)。誰か助けてくれますか?

答えて

0

私は全くわからないんだけど、私は、これは動作するはずだと思う:

<% if user_signed_in? %> 
<%= form_for current_user, url: edit_user_registration_path do |f| %> 
    <div class="form-group"> 
     <%= f.collection_select(:current_company, current_user.moderated_companies, :id, :name, prompt: "Select a company") %> 
    <div class="actions"> 
     <%= f.submit %> 
    </div> 
    <% end %> 
<% end %> 

はそれが動作するかどうか、私はしかし、知ってみましょう!私はあなたが使用するように変更すべきだと思う

UPDATE

#user.rb 
has_many :moderated_companies, through: :moderator_connections, source: :company 

#company.rb 
has_many :moderators, through: :moderator_connections, source: :user 
+0

親愛なる@oreoluwa、お返事ありがとうございました。新しいエラーが発生しました: 'ModeratorConnectionモデルのソースアソシエーション「moderated_company」または:moderated_companiesを見つけることができませんでした。 'has_many:moderated_companies、:through =>:moderator_connections、:source =>'を試してください。それはユーザーか会社のどちらかですか? ' 私の団体はまだ適切に調整されていないようです。適切なassiociationsを追加する方法に関する任意のアイデア? –

+0

私の答えを更新しました、うまくいくことを祈ってください – oreoluwa

+0

はい、はるかに良い!それでもエラー( 'ルートは[PATCH]と一致しません"/users/edit "')です。ルートに手動でパッチルートを追加する必要がありますか? –

関連する問題