Reputation: 7
I'm new to rails and experimenting. After I managed to do this, I was now trying to do the following:
when clicking on a link for a setting's name, it's description should be rendered in a partial on the same page. I managed to render the partial, but only if I put some static text in it. For some reason I'm not able to pass the @fbs variable and thus to select the descriptions. I tried many different suggestions on other similar questions but I really don't get how I should do it. I'm using ruby 2.4.0 and rails 5.1
Here is my _settingsfb.html.erb (which is a partial of index.html.erb and which contains the 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>
here the settingsfb_controller (the relevant part)
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
the show.js.erb
$("#desc").html("<%= j render 'settingsfb/show' %>");
and the _show partial
<%= @fbs.name %>
this is what I get when I click on a the link
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'
any Ideas?
Upvotes: 0
Views: 1012
Reputation: 1846
You should initialize @fbs
before rendering after show action. So add @fbs = Setting.find(params[:id])
at the beginning of the show method.
Upvotes: 1
Reputation: 2927
The error indicates there is no @fbs variable. You're calling the settingsFbShow partial from the show.js.erb view but not passing any variables. Try
$("#desc").html("<%= j render 'settingsfb/show', locals: {fbs: fbs} %>");
Then in create.js.erb
<%= fbs.name %>
Upvotes: 0