2017-05-14 102 views
0

我是新來的rails和試驗。在我設法做了this之後,我正在嘗試執行以下操作:rails 5.1:將變量傳遞給partial通過ajax

當點擊設置名稱的鏈接時,它的描述應該在同一頁面上呈現爲部分。我設法渲染了部分內容,但前提是我在其中添加了一些靜態文本。出於某種原因,我無法傳遞@fbs變量,從而選擇描述。我對其他類似問題嘗試了許多不同的建議,但我真的不明白我應該怎麼做。我使用Ruby 2.4.0和5.1導軌

這裏是我的_settingsfb.html.erb(這是部分index.html.erb的和包含LINK_TO)

<div class="container fluid"> 
    <div class="row"> 


    <!-- settings links --> 
    <div class="col-md-4 "> 
<div class="panel panel-default"> 
    <div class="panel-heading"> 
    <h3 class="panel-title">Settings</h3> 
    </div> 
    <div class="panel-body"> 
    <% @fbs.each do |fbs| %> 
<ul> 
     <li> <%= link_to fbs.name, controller: 'settingsfb', action: 'show', id: fbs.id,remote: true %></li> 
     </ul><% end %> 
    </div> 
</div> 
</div> 

    <!-- setting description --> 

    <div class="col-md-4"> 
<div class="panel panel-default"> 
    <div class="panel-heading"> 
    <h3 class="panel-title">Description</h3> 
    </div> 
    <div class="panel-body"> 
    <div id="desc"> 
     <!-- here goes the description partial --> 
    </div> 
    </div> 
</div> 
</div> 

這裏settingsfb_controller (相關部分)

class SettingsfbController < ApplicationController 
    before_action :set_setting, only: [:show, :edit, :update, :destroy] 

    # GET /settings 
    # GET /settings.json 
    def index 
    @settings = Setting.all 
    @fbs = Setting.where("Sns = 1") 
    end 



    # GET /settings/1 
    # GET /settings/1.json 
    def show 
     respond_to do |format| 
     format.js {render layout: false} 
    end 
    end 

的show.js.erb

$("#desc").html("<%= j render 'settingsfb/show' %>"); 

和_show局部

<%= @fbs.name %> 

這就是當我點擊一個鏈接

Started GET "/settingsfb/show?id=1" for 127.0.0.1 at 2017-05-14 17:08:34 +0200 
    Processing by SettingsfbController#show as JS 
     Parameters: {"id"=>"1"} 
     Setting Load (0.3ms) SELECT "settings".* FROM "settings" WHERE "settings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 
     Rendering settingsfb/show.js.erb 
     Rendered settingsfb/_show.html.erb (2.6ms) 
     Rendered settingsfb/show.js.erb (3.8ms) 
    Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.3ms) 



    ActionView::Template::Error (undefined method `name' for nil:NilClass): 
     1: <%= @fbs.name %> 

    app/views/settingsfb/_show.html.erb:1:in `_app_views_settingsfb__show_html_erb___617047289144576595_70310163068960' 
    app/views/settingsfb/show.js.erb:1:in `_app_views_settingsfb_show_js_erb___3755597588635490219_70310163079560' 
    app/controllers/settingsfb_controller.rb:17:in `block (2 levels) in show' 
    app/controllers/settingsfb_controller.rb:16:in `show' 

任何想法我得到什麼?

+0

顯示您的:set_setting方法 –

+0

'#使用回調共享操作之間的常見設置或約束。 高清set_setting @setting = Setting.find(PARAMS [:編號]) end' @AlexanderSvetly – engid87

+0

我已經嘗試過,初始化'@ fbs'在控制器中的表演動作的開始,但隨後我的部分呈現「設置「而不是」設置nr 1「(這將是''fbs.name的正確輸出)' – engid87

回答

1

表演動作後呈現之前,您應該初始化@fbs。所以在show方法的開頭添加@fbs = Setting.find(params[:id])

0

該錯誤表示沒有@fbs變量。您從show.js.erb視圖調用settingsFbShow部分,但不傳遞任何變量。嘗試

$("#desc").html("<%= j render 'settingsfb/show', locals: {fbs: fbs} %>"); 

然後在create.js.erb

<%= fbs.name %> 
+0

我試過了,但沒有改變 – engid87