2016-11-21 1 views
0

私はこのような何か(基本的なドロップダウン・メニュー)があります。デフォルトのレールからデータを取り出す方法:データハッシュ?

<ul class="dropdown-menu"> 
 
    <% @sidebar_categories.each do |category| %> 
 
    <li data-toggle="modal" data-target="#howtoFormModal"> 
 
     <%= link_to category.title, "#", :data => { :id => category.id } %> 
 
    </li> 
 
    <% end %> 
 
</ul>

そして、私は、モーダル

<%= simple_form_for @howto, url: category_howtos_path(@category) do |f| %> 
 
    #whatever here... 
 
<% end %>

と私なりの内側にあるフォームを持っているが既定のレールで以前に渡したidを取得します:データハッシュとidをフォームurlに配置するcategory_howtos_path (IDはここに行く)

どうすればいいですか?

答えて

1

あなたが達成しようとしていることを100%確信しているわけではありませんが、おそらくクエリーストリングを使用することができます。docs on link_toにはあなたがそうする方法の例があります。あなたの例ではそう

このような何か:あなたは切り替えたい最初のスニペットでli上の属性によって

<ul class="dropdown-menu"> 
    <% @sidebar_categories.each do |category| %> 
    <li data-toggle="modal" data-target="#howtoFormModal"> 
     <%= link_to category.title, some_path(id: category.id) %> 
    </li> 
    <% end %> 
</ul> 
# Which has an output like this 

<a href="/some-path?id=1">Category Title</a> 

そして...

<%= simple_form_for @howto, url: category_howtos_path(params[:id]) do |f| %> 
    #whatever here... 
<% end %> 

編集

クリックでモーダルになり、次にsimple_form_for URLパラメータに切り替えられたモーダルのIDを持ちます。問題は、category_howtos_pathがルビのページ読み込み前に評価されていることです。したがって、そのURLを変更したい場合は、javascriptを使用することができます。だから多分javascriptでモーダルをロードし、イベントコールバックを持っています。 JSと

EDITは

スニペットええ、私はすでに試みたが、多分私はそれを間違った方法をしました。

$('#howtoFormModal').on('show.bs.modal', function (e) { 

     //but I think that the JS will give a lot due to the rails loop 
     var categoryId = $("#li-id").data('id') 

     // just to show the id on the modal 
     $('#howtoFormModalLabel').val(categoryId); 
}); 

あなたは)あなたがそう

$('#howtoFormModal').on('show.bs.modal', function (e) { 

    // The categoryId from here needs to be from the clicked on element 
    var categoryId = $(e.relatedTarget).data('id') 

    // just to show the id on the modal 
    $('#howtoFormModalLabel').val(categoryId); 

}をクリックした要素のidを取得したいと思います。

+0

以前は、別のページのフォームであったからです。しかし今はモーダルの中にあるので、別のページに行く必要はもうありません。あなたの提案に感謝 – NeimadTL

+0

ああ私は私の答えを更新しました –

+0

私はおそらく助けることができるjsコードであなたの質問を更新する場合は? –

関連する問題