Ruby on Rails - 表单不填充哈希 - 仅在heroku中,在本地/开发环境中正常工作

时间:2016-01-10 02:31:46

标签: ruby-on-rails ruby heroku polymorphic-associations form-for

继续我的传奇,创建一个名为address的多态对象,它与帐户和联系人有一对多的关系(以及将来的其他对象)。

Ruby Polymorphic association working locally but not in heroku

show account视图应该使用多态数据(addressable_id,addressable_type)将参数发送到new_address,在这种情况下是account_id和帐户控制器。

show帐户有以下链接来创建新地址:

<%= 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 %>

转发网址后显示:

http://vulcanosys.herokuapp.com/addresses/new?addressable_id=1&addressable_type=accounts

表单字段是隐藏的,所以首先我认为这是创建操作中的一个问题,但在设置字段可见后,我可以看到问题是heroku没有使用hash参数填充表单。

虽然在localhost中它完全加载,但网址也显示:

    http://localhost:3000/addresses/new?addressable_id=1&addressable_type=accounts

地址/ new.html.erb是以下

    <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>

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

从帐户创建新地址(完美地运行)时,localhost记录如下:

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)

在heroku中这是行为:

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

我在这方面挣扎了一个星期,不知道该怎么做,我甚至在思考硬编码的多态关联。

1 个答案:

答案 0 :(得分:0)

我解决了上述问题,执行了以下步骤:

将带有浅层的多态模型地址的嵌套路由应用于忽略编辑,更新和删除操作的关联。

    shallow do
       resources :accounts do
           resources :addresses
       end
    end

    shallow do
       resources :contacts do
          resources :addresses
       end
    end

我还在应用程序未完全投入生产时更改了一些设置。

Config.application.enviroment.production

    config.assets.digest = true
    config.eager_load = false