我想將params從一個視圖傳遞到另一個視圖。在我的index.html.erb我有一個空div:控制器沒有在rails中調用
index.html.erb:
<div id="main"> </div>
首先我通過以下加載側欄內的main.html.erb文件鏈接:
side.html.erb
<%= link_to image_tag('btn_next.png'), home_main_path(:game_id=>item.game.id), :remote => true %>
這是我的加載文件main.html.erb進入主DIV 。下面是一些代碼:
main.js.erb
$("#main").html("<%= escape_javascript render(:file => 'home/main.html.erb', :handlers => [:erb]) %>");
home_controller.rb
def main
puts 'main called'
...
日誌
main called
Started GET "/home/main?game_id=1" for 127.0.0.1 at 2016-02-15 18:54:37 +0100
Processing by HomeController#main as JS
Parameters: {"game_id"=>"1"}
Ë一切都很好。現在,該main.html.erb裏面我有一個鏈接到一個game_main.html.erb文件:
main.html.erb
<%= link_to I18n.t('basic.game_statistic'), home_game_main_path(:game_id => @game.id, :mode => 'GameGoals'), :remote => true, :class => "main" %>
這裏的game_main鏈接一些代碼:
game_main.js.erb
$("#main").html("<%= escape_javascript render(:file => 'home/game_main.html.erb', :handlers => [:erb]) %>");
home_controller.rb
def game_main
puts 'game_main called'
...
html文件被加載到main-div中。但是game_main-controller沒有被調用。我的日誌:
日誌
Started GET "/home/game_main?game_id=1&mode=GameGoals" for 127.0.0.1 at 2016-02-15 19:13:19 +0100
Processing by HomeController#game_main as JS
Parameters: {"game_id"=>"1", "mode"=>"GameGoals"}
不通知 'game_main稱爲'。所以我無法獲得參數。什麼地方出了錯?我能夠處理HomeController#main,但不能處理HomeController#game_main。
感謝您的幫助。
拉爾斯
P.S:我的路線:
路線。RB
match '/home/main', :to => 'home#main'
match '/home/game_main', :to => 'home#game_main'
耙路線
home_main /home/main(.:format) home#main
home_game_main /home/game_main(.:format) home#game_main
但它與main.html.erb文件?!任何建議如何編碼? – Lars
我試着用<%= link_to I18n.t('basic.game_statistic'),home_game_main_path(:game_id => @ game.id,:mode =>'GameGoals'),方法:: get,:controller =>: home_controller,:action =>:game_main,:remote => true,:class =>「main」%> - 不工作 – Lars