0
下面的問題類似的問題在當地,但沒有工作,但需要不同的解決方案紅寶石多態性協會的Heroku
ActionView::Template::Error (undefined method...) Works locally, but not on Heroku
我多態關聯地址>帳號在本地工作,但是當我把它上傳到的Heroku
Rendered addresses/show.html.erb within layouts/application (101.1ms)
2016-01-09T03:04:14.947013+00:00 app[web.1]:
2016-01-09T03:04:14.947015+00:00 app[web.1]: ActionView::Template::Error (undefined method `addressable' for nil:NilClass):
2016-01-09T03:04:14.947016+00:00 app[web.1]: 1: <div id="central">
2016-01-09T03:04:14.947017+00:00 app[web.1]: 2: <h1><%= @addresses.addressable.try(:name) %></h1>
2016-01-09T03:04:14.947018+00:00 app[web.1]: 3: <h5><%= @addresses.addressable_type %> Address </h5>
2016-01-09T03:04:14.947018+00:00 app[web.1]: 4: <div id="limiter">
2016-01-09T03:04:14.947020+00:00 app[web.1]: app/views/addresses/show.html.erb:2:in `_app_views_addresses_show_html_erb__1277570398496108107_69968312940660'
2016-01-09T03:04:14.947019+00:00 app[web.1]: 5: <table class="tbig" align="center" style="width:100%;">
2016-01-09T03:04:14.947021+00:00 app[web.1]:
2016-01-09T03:04:14.947021+00:00 app[web.1]:
的模型設置是這樣的::
渲染視圖時,我得到了以下錯誤數據庫表等:
class CreateTableAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :no
t.string :street
t.string :suburb
t.string :code
t.text :details
t.belongs_to :city
t.references :addressable, polymorphic: true, index: true
t.timestamps null: false
end
end
end
class CreateTableAccounts < ActiveRecord::Migration
def change
create_table :accounts do |t|
t.text :name
t.string :no
t.string :site
t.string :email
t.string :phone1
t.string :phone2
t.text :details
t.timestamps null: false
end
end
end
的地址視圖從show_accounts
<div id="central">
<h1><%= @addresses.addressable.try(:name) %></h1>
<h5><%= @addresses.addressable_type %> Address </h5>
<div id="limiter">
<table class="tbig" align="center" style="width:100%;">
<col width="50%">
<col width="50%">
<tr><th colspan="2" class="bottom">Address Details</th></tr>
<tr class="odd">
<td class="left">Address System ID: </td>
<td class="right output"> <%= @addresses.id %> </td>
</tr>
<tr class="even">
<td class="left">Number: </td>
<td class="right output"> <%= @addresses.no %> </td>
</tr>
<tr class="odd">
<td class="left">Street: </td>
<td class="right output"><%= @addresses.street %></td>
</tr>
<tr class="even">
<td class="left">Suburb:</td>
<td class="right output"><%= @addresses.suburb %></td>
</tr>
<tr class="odd">
<td class="left">Post Code: </td>
<td class="right output"><%= @addresses.code %></td>
</tr>
<tr class="even">
<td colspan="2" class="bottom"> <%= @addresses.details %></td> </tr>
<tr><th colspan="2" class="bottom"></th></tr>
</table>
<div id="menu">
<ul class="menu">
<!-- Access Check - Edit Address -->
<% if Accessrule.find_by(role_id: current_user.role_id, workspace:2).try(:canupdate?) %>
<li> <%= link_to (image_tag("address_edit.png", :width => "70%")), edit_address_path %>
<br /><%= link_to "Edit Address", edit_address_path %></li>
<% end %>
<!-- back Accounts -->
<li> <%= link_to (image_tag("address_back.png", :width => "70%")), @addresses.addressable %>
<br /><%= link_to "Back", @addresses.addressable %></li>
</ul>
</div>
</div>
和地址控制器到來時被接收到錯誤:
類AddressesController < ApplicationController的 before_action :logged_in_user
def address_params
params.require(:address).permit(:id, :no, :street, :suburb, :code, :details, :city_id, :addressable_type, :addressable_id)
end
def new
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New")
@addresses = Address.new(:addressable_type => params[:addressable_type],:addressable_id => params[:addressable_id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New !Access Denied! ")
flash[:notice] = "You don't have access to create Addresses."
redirect_to :back
end
end
def create
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create", details: address_params)
@address = Address.new(address_params)
if @address.save
flash[:notice] = 'Address Saved'
redirect_to @address.addressable
else
render "new"
end
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create !Access Denied! ")
flash[:notice] = "You don't have access to create Addresses."
redirect_to :back
end
end
def edit
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit", details: params[:id])
@addresses = Address.find(params[:id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit !Access Denied! ", details: params[:id])
flash[:notice] = "You don't have access to edit Addresses."
redirect_to :back
end
end
def update
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Update", details: address_params)
@accounts = Account.all
@addresses = Address.find(params[:id])
if @addresses.update_attributes(address_params)
redirect_to @address.addressable
flash[:notice] = 'Account Updated'
else
render "edit"
flash[:error]
end
else
flash[:notice] = "You don't have access to edit Addresss."
redirect_to back
end
end
def show
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canread?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View", details: params[:id])
@addresses = Address.find(params[:id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View !Access Denied! ", details: params[:id])
flash[:notice] = "You don't have access to view Addresss."
redirect_to back
end
end
def destroy
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:candelete?)
@addresses = Address.find(params[:id])
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Delete", details: params[:id])
@addresses.destroy
redirect_to :back
flash[:notice] = 'Address Deleted'
else
flash[:notice] = "You don't have access to delete Addresss."
redirect_to :back
end
end
# Before Filters
# Confirms if User is logged-in
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to root_path
end
end
end
但該記錄退出,並且可由使用生產數據庫的本地服務器訪問。即使我試圖通過url顯示視圖,我也會得到相同的錯誤。 –
事實是'Address.find'返回nil。你如何將本地連接到生產數據庫?嘗試打開導軌控制檯('heroku run rails c')並檢查你會到達那裏。另外:你在show方法的底部有'back'而不是':back'。 – BroiSatse
等一下,什麼? :)命名只是命名,它可能是問題的唯一方法是當你在控制器和視圖中的變量不同時,但他們似乎是正確的。它的作用實際上是一個更大問題的症狀。 – BroiSatse