PiRSquared17 / activescaffold

Automatically exported from code.google.com/p/activescaffold
MIT License
0 stars 0 forks source link

routing error if field id is NULL #773

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use a Table with an customized "id" filed. in this Table is also a rolumn 
with Name id, wich content is NULL
2. Make a common active_scaffold from this, with the correct routing
3. Use it

What is the expected output? What do you see instead?

expected:
The common, nice active scaffold Display of the Table

instead:
A routing error:

Started GET "/interfaces" for 10.32.222.22 at Wed Nov 09 18:20:36 +0100 2011
  Processing by InterfacesController#index as HTML
   (1.6ms)  SELECT COUNT(*) FROM `tbl_if` 
  Interface Load (2.0ms)  SELECT `tbl_if`.* FROM `tbl_if` ORDER BY `tbl_if`.`if_id` ASC LIMIT 15 OFFSET 0
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_act
ion_group.html.erb (2.6ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t_header.html.erb (9.0ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t_column_headings.html.erb (2.9ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_mes
sages.html.erb (1.0ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t_messages.html.erb (113.0ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t_record.html.erb (2.9ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t.html.erb (133.1ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/_lis
t_with_header.html.erb (151.0ms)
Rendered 
/usr/lib64/ruby/gems/1.8/gems/active_scaffold-3.1.7/frontends/default/views/list
.html.erb within layouts/application (155.2ms)
Completed 500 Internal Server Error in 175ms

ActionView::Template::Error (No route matches {:controller=>"interfaces", 
:action=>"row", :escape=>false, :_method=>:get}):
    7: action_links ||= active_scaffold_config.action_links.member
    8: -%>
    9: 
    10: <tr class="record <%= tr_class %>" id="<%= element_row_id(:action => :list, :id => record.id) %>" data-refresh="<%= url_for(params_for(:action => :row, :id => record.id, :_method => :get, :escape => false)).html_safe %>">
    11:   <%= render :partial => 'list_record_columns', :locals => {:record => record, :columns => columns} %>
    12:   <%= render :partial => 'list_actions', :locals => {:record => record, :url_options => url_options, :action_links => action_links} unless action_links.empty? %>
    13:   <%= render_nested_view(action_links, url_options, record) unless @nested_auto_open.nil? %>

Rendered 
/usr/lib64/ruby/gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/middleware/te
mplates/rescues/routing_error.erb within rescues/layout (0.7ms)

What version (or revision) of the product are you using?
Rails 3.1.7
Active_scaffold 3.1.7
mysql 2.8.1

Solution (sort of)

In the Model:

  def id
    if read_attribute(:id) == nil then
        "NULL"
    else
        read_attribute(:id)
    end
  end

If this bug causes an exception, please paste at least the first 20 lines
below of a full backtrace (with backtrace silencers removed).

Original issue reported on code.google.com by jheb...@gmail.com on 10 Nov 2011 at 6:12

GoogleCodeExporter commented 9 years ago
id method in rails must return primary_key. This is the rails code for id 
method:
def id
  attributes[self.class.primary_key]
end

You are overriding id method and breaking active scaffold
You can set another primary key instead of id, but you can't add another id 
field.

Original comment by sergio.c...@gmail.com on 10 Nov 2011 at 9:47

GoogleCodeExporter commented 9 years ago
Hmm, its hard for me to explain, because english is not ny native tongue. Next 
try:

Given this Table(bont blame me for this, it's an legacy app):

mysql> desc tbl_if;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| if_id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| node_id     | int(11)     | YES  | MUL | 0       |                |
| bez         | varchar(50) | YES  |     | NULL    |                |
| not_host_IF | tinyint(4)  | YES  |     | 0       |                |
| art         | tinyint(4)  | YES  |     | 0       |                |
| MAC         | varchar(20) | YES  |     | NULL    |                |
| vart        | varchar(30) | YES  |     | NULL    |                |
| id-typ      | varchar(20) | YES  |     | NULL    |                |
| id          | varchar(80) | YES  |     | NULL    |                |
| connected   | tinyint(4)  | YES  |     | 0       |                |
+-------------+-------------+------+-----+---------+----------------+

whith this content:

mysql> select * from  tbl_if LIMIT 10;
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+
| if_id | node_id | bez  | not_host_IF | art  | MAC  | vart   | id-typ | id     
           | connected |
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+
|     1 |     260 | ALOM |           0 |    1 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     2 |     260 | ce0  |           0 |    1 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     3 |     260 | ce1  |           0 |    1 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     4 |     246 | eth0 |           0 |    0 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     5 |     247 | eth0 |           0 |    0 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     6 |     248 | eth0 |           0 |    0 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     7 |     249 | eth0 |           0 |    0 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|     8 |       1 | eth0 |           0 |    0 | NULL | Net-Cu | MAC    | NULL   
           |         0 |
|    11 |     262 | ce0  |           0 |    1 | NULL | Net-Cu | MAC    | 
00:03:ba:85:5c:21 |        -1 |
|    12 |     262 | ce1  |           0 |    1 | NULL | Net-Cu | MAC    | 
00:03:ba:85:5c:22 |        -1 |
+-------+---------+------+-------------+------+------+--------+--------+--------
-----------+-----------+

and this Model:

class Interface < MysqlDatabase
  set_primary_key "if_id"
  set_table_name "tbl_if"

  belongs_to :system, :foreign_key => "node_id"
  has_many :ips, :foreign_key => :if_id

  def to_label
    bez
  end

end

active_scaffold fails with the routing error mentioned above. but if one 
add this to the model:

  def id 
    if read_attribute(:id) == nil then
        "NULL"
    else        
        read_attribute(:id)
    end
  end

active_scaffold renders the data. With one exception. I hav found no way to 
display
the content of the field "id"

Jan

Original comment by jheb...@gmail.com on 11 Nov 2011 at 6:20

GoogleCodeExporter commented 9 years ago
I had understood you, but you cannot add id field, because adding id field you 
break this ActiveRecord method:

def id
  attributes[self.class.primary_key]
end

ActiveScaffold uses that rails method to get the "id" (if_id in your case) for 
the record.
With your workaround you are confusing ActiveScaffold, which build routes using 
the id field instead of primary key

Original comment by sergio.c...@gmail.com on 11 Nov 2011 at 9:58