Continuing on my saga to create a polymorphic object called address which has a one to many relationship with accounts and contacts (and in the future for others objects).
Ruby Polymorphic association working locally but not in heroku
The show account view should send parameters to new_address with the polymorphic data(addressable_id,addressable_type) which in this case is the account_id and the account controller.
The show account have the following link to create new address:
<%= link_to new_address_path(:controller => "addresses", :action => "new", :addressable_type => :account, :addressable_id => @accounts.id, ) do %>
<%= (image_tag("address_new.png", :width => "70%")) %>
<br /> New Address
<% end %>
And after forwarding the url shows:
http://vulcanosys.herokuapp.com/addresses/new?addressable_id=1&addressable_type=accounts
The form fields were hidden, so first of all I thought it was a problem in the create action, but after setting fields visible, I can see that the issue is that heroku is not populating the form with the parameter on hash.
While in the localhost it loads perfectly and the urls also shows:
http://localhost:3000/addresses/new?addressable_id=1&addressable_type=accounts
the addresses/new.html.erb is the following
<div id="central">
<div id="limiter">
<h1><%= (image_tag("address_new.png", :width => "20%", :align =>"middle")) %> New Address</h1>
<h4>Please insert Account data:</h4>
<%= form_for @addresses, url: {action: "create"} do |f| %>
<table class="tshow" align="left" style="width:100%;">
<col width="50%">
<col width="50%">
<%= f.text_field :addressable_type %>
<%= f.text_field :addressable_id %>
<tr class="even">
<td><%= f.label :no, "No:" %>*</td>
<td><%= f.text_field :no %></td>
</tr>
<tr class="odd">
<td><%= f.label :street, "Street:" %>*</td>
<td><%= f.text_field :street %></td>
</tr>
<tr class="even">
<td><%= f.label :suburb, "Suburb" %>*</td>
<td><%= f.text_field :suburb %></td>
</tr>
<tr class="odd">
<td><%= f.label :code, "Zip:" %>*</td>
<td><%= f.text_field :code %></td>
</tr>
</table>
</div>
<div id="menu">
<ul class="menu">
<!-- Access Check - Create New Account -->
<% if Accessrule.find_by(role_id: current_user.role_id, workspace:2).try(:canwrite?) %>
<li><%= f.submit " ", :type => :image, :src => image_path("address_save.png"), :width =>"60%" %>
<br /> <%= f.submit "Save Address", class: "savetext" %><% end %></li>
<% end %>
<!-- back Accounts -->
<li> <%= link_to :back do %>
<%= (image_tag("address_back.png", :width => "60%")) %>
<br /> Back
<% end %>
</li>
</li>
</ul>
</div>
</div>
The addresses_controller
class AddressesController < ApplicationController
before_action :logged_in_user
def address_params
params.require(:address).permit(:id, :no, :street, :suburb, :code, :details, :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)
@addresses = Address.new(address_params)
if @addresses.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])
@address = 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
@address = Address.find(params[:id])
if @address.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])
@address = 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?)
@address = 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])
@address.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
The localhost logs when creating new address from account (which works perfectly) are below:
Started GET "/addresses/new?addressable_id=1&addressable_type=account" for 127.0.0.1 at 2016-01-10 13:16:09 +1100
Started GET "/addresses/new?addressable_id=1&addressable_type=account" for 127.0.0.1 at 2016-01-10 13:16:09 +1100
Processing by AddressesController#new as HTML
Processing by AddressesController#new as HTML
Parameters: {"addressable_id"=>"1", "addressable_type"=>"account"}
Parameters: {"addressable_id"=>"1", "addressable_type"=>"account"}
User Load (260.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (260.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Accessrule Load (261.2ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 3 LIMIT 1 [["role_id", 1]]
Accessrule Load (261.2ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 3 LIMIT 1 [["role_id", 1]]
(259.7ms) BEGIN
(259.7ms) BEGIN
SQL (261.6ms) INSERT INTO "logs" ("user_id", "role_id", "workspace_id", "action", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["role_id", 1], ["workspace_id", 3], ["action", "New"], ["created_at", "2016-01-10 02:16:10.427199"]]
SQL (261.6ms) INSERT INTO "logs" ("user_id", "role_id", "workspace_id", "action", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["role_id", 1], ["workspace_id", 3], ["action", "New"], ["created_at", "2016-01-10 02:16:10.427199"]]
(261.8ms) COMMIT
(261.8ms) COMMIT
Accessrule Load (261.6ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 2 LIMIT 1 [["role_id", 1]]
Accessrule Load (261.6ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 2 LIMIT 1 [["role_id", 1]]
Rendered addresses/new.html.erb within layouts/application (273.2ms)
Rendered addresses/new.html.erb within layouts/application (273.2ms)
Rendered layouts/_header.html.erb (2.5ms)
Rendered layouts/_header.html.erb (2.5ms)
Completed 200 OK in 1673ms (Views: 99.7ms | ActiveRecord: 1566.9ms)
Completed 200 OK in 1673ms (Views: 99.7ms | ActiveRecord: 1566.9ms)
while in heroku this is the behavior:
2016-01-10T02:22:50.293725+00:00 heroku[router]: at=info method=GET path="/addresses/new?addressable_id=1&addressable_type=account" host=vulcanosys.herokuapp.com request_id=f09fa641-f8b4-4546-9c45-6cad598c0759 fwd="110.174.67.117" dyno=web.1 connect=13ms service=30ms status=200 bytes=4678
2016-01-10T02:22:50.274168+00:00 app[web.1]: Started GET "/addresses/new?addressable_id=1&addressable_type=account" for 110.174.67.117 at 2016-01-10 02:22:50 +0000
2016-01-10T02:22:50.279522+00:00 app[web.1]: Accessrule Load (0.6ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 3 LIMIT 1 [["role_id", 1]]
2016-01-10T02:22:50.282693+00:00 app[web.1]: SQL (0.5ms) INSERT INTO "logs" ("user_id", "role_id", "workspace_id", "action", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["role_id", 1], ["workspace_id", 3], ["action", "New"], ["created_at", "2016-01-10 02:22:50.281069"]]
2016-01-10T02:22:50.288683+00:00 app[web.1]: Accessrule Load (0.4ms) SELECT "accessrules".* FROM "accessrules" WHERE "accessrules"."role_id" = $1 AND "accessrules"."workspace_id" = 2 LIMIT 1 [["role_id", 1]]
2016-01-10T02:22:50.291283+00:00 app[web.1]: Rendered layouts/_header.html.erb (0.5ms)
2016-01-10T02:22:50.276049+00:00 app[web.1]: Processing by AddressesController#new as HTML
2016-01-10T02:22:50.276089+00:00 app[web.1]: Parameters: {"addressable_id"=>"1", "addressable_type"=>"account"}
2016-01-10T02:22:50.277601+00:00 app[web.1]: User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
2016-01-10T02:22:50.280790+00:00 app[web.1]: (0.4ms) BEGIN
2016-01-10T02:22:50.284721+00:00 app[web.1]: (1.4ms) COMMIT
2016-01-10T02:22:50.290048+00:00 app[web.1]: Rendered addresses/new.html.erb within layouts/application (4.3ms)
2016-01-10T02:22:50.292342+00:00 app[web.1]: Completed 200 OK in 16ms (Views: 6.6ms | ActiveRecord: 3.9ms)
2016-01-10T02:22:50.586660+00:00 heroku[router]: at=info method=GET path="/assets/main-e887d4be5a3761150a9e75e8cdbe02be8766d91d0d6fce21a598f9e45ee40bf3.css" host=vulcanosys.herokuapp.com request_id=c2aab484-4cb3-47fc-9d67-eb340f685929 fwd="110.174.67.117" dyno=web.1 connect=3ms service=5ms status=304 bytes=133
2016-01-10T02:22:50.606720+00:00 heroku[router]: at=info method=GET path="/assets/logow-bd7c174b4f1a44bb00b4fc7c15a98c7bfd339679bf9c2d101b2083369a657fc4.png" host=vulcanosys.herokuapp.com request_id=8f40c3f4-e5e6-4e0d-80fa-c38c0dba4369 fwd="110.174.67.117" dyno=web.1 connect=2ms service=7ms status=304 bytes=133
2016-01-10T02:22:50.609673+00:00 heroku[router]: at=info method=GET path="/assets/application-79669da6f547131a23184bf35af9884a3f36dac244c97e2686166cd04e33a046.js" host=vulcanosys.herokuapp.com request_id=5c1be766-73ff-4242-a6e0-263e2c194ba8 fwd="110.174.67.117" dyno=web.1 connect=15ms service=8ms status=304 bytes=133
2016-01-10T02:22:50.574276+00:00 heroku[router]: at=info method=GET path="/assets/tables-fa95ff6a0b1550437c004e8f43db0f1015ceb7461f5e91a53457daa6d06123ab.css" host=vulcanosys.herokuapp.com request_id=d04461f7-d118-41d7-b7f4-c963503590bd fwd="110.174.67.117" dyno=web.1 connect=2ms service=8ms status=304 bytes=133
2016-01-10T02:22:50.925131+00:00 heroku[router]: at=info method=GET path="/assets/logof-94cafb7b2caedd8d7695e584b5ef3055aa023d772104795b8953a6d478e09bc2.png" host=vulcanosys.herokuapp.com request_id=e418984d-f4c4-454d-8423-ba857b82b897 fwd="110.174.67.117" dyno=web.1 connect=2ms service=5ms status=304 bytes=133
I'm struggling for a week on this, dont know what to do, I'm even thinking about hard code the polymorphic association.
I solved the above issue performing the following steps:
Applied nested routes to the polymorphic model address with shallow, which ignores the association for edit, update and delete action.
shallow do
resources :accounts do
resources :addresses
end
end
shallow do
resources :contacts do
resources :addresses
end
end
I also have changed some settings while application is not fully in production.
Config.application.enviroment.production
config.assets.digest = true
config.eager_load = false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.