在开发一个小功能,controller 是 fun_urls_controller.rb,model 是 fun_url.rb Controller 的代码:
#encoding = utf-8
2
3 class FunUrlsController < ApplicationController
4 def index
5 @fun_urls = FunUrl.all
6 end
7
8 def new
9 @fun_url = FunUrl.new
10 end
11
12 def create
13 @fun_url = FunUrl.new(params[:fun_url])
14 if @fun_url.save
15 redirect_to fun_urls_url, :notice => 'URL添加成功'
16 else
17 render :action => :new
18 end
19 end
20
21 def edit
22 @fun_url = FunUrl.find(params[:id])
23 end
24
25 def update
26 @fun_url = FunUrl.find(params[:id])
27 if @fun_url.update_attributes(params[:fun_url])
28 redirect_to fun_urls_url, :notice => 'URL修改成功'
29 else
30 render :action => :edit
31 end
32 end
33
34 def destroy
35 @fun_url = FunUrl.find(params[:id])
36
37 @fun_url.destroy
38
39 redirect_to fun_urls_url, :alert => 'URL删除成功'
40 end
41 end
index 页面的代码:
1 <article class="module width_full">
2 <header>
3 <h3 class="tabs_involved">URL列表</h3>
4 <ul class="tabs">
5 <li><%= link_to '添加', new_fun_url_path %></li>
6 </ul>
7 </header>
8 <table class="tablesorter" cellspacing="0">
9 <thead>
10 <tr>
11 <th></th>
12 <th>编码</th>
13 <th>URL</th>
14 <th>方法</th>
15 <th>操作</th>
16 </tr>
17 </thead>
18 <tbody>
19 <% @fun_urls.each do |fun_url| %>
20 <tr>
21 <td></td>
22 <td><%= fun_url.code %></td>
23 <td><%= fun_url.url %></td>
24 <td><%= fun_url.method %></td>
25 <td>
26 <%= link_to image_tag('icn_edit.png', :border => 0), edit_fun_url_path(fun_url) %>
27 </td>
28 </tr>
29 <% end %>
30 </tbody>
31 </table>
32 </article>
如果没有<%= link_to image_tag('icn_edit.png', :border => 0), edit_fun_url_path(fun_url) 这句,页面能正常显示,且能列出数据,如果加上这句,就会报异常:
Showing /home/puras/puras/rails/demo/app/views/fun_urls/index.html.erb where line #26 raised:
undefined method `join' for nil:NilClass
Extracted source (around line #26):
23: <td><%= fun_url.url %></td>
24: <td><%= fun_url.method %></td>
25: <td>
26: <%= link_to image_tag('icn_edit.png', :border => 0), edit_fun_url_path(fun_url) %>
27: </td>
28: </tr>
29: <% end %>
执行 rake routes 后,能查看到查应的 URL 映射:
fun_urls GET /fun_urls(.:format) fun_urls#index
POST /fun_urls(.:format) fun_urls#create
new_fun_url GET /fun_urls/new(.:format) fun_urls#new
edit_fun_url GET /fun_urls/:id/edit(.:format) fun_urls#edit
fun_url GET /fun_urls/:id(.:format) fun_urls#show
PUT /fun_urls/:id(.:format) fun_urls#update
DELETE /fun_urls/:id(.:format) fun_urls#destroy
后台日志是:
Started GET "/fun_urls" for 127.0.0.1 at 2012-05-18 12:50:30 +0800
Processing by FunUrlsController#index as HTML
FunUrl Load (0.1ms) SELECT `fun_urls`.* FROM `fun_urls`
Rendered fun_urls/index.html.erb within layouts/application (2.2ms)
Completed 500 Internal Server Error in 5ms
ActionView::Template::Error (undefined method `join' for nil:NilClass):
23: <td><%= fun_url.url %></td>
24: <td><%= fun_url.method %></td>
25: <td>
26: <%= link_to 'aa', edit_fun_url_path(fun_url) %>
27: </td>
28: </tr>
29: <% end %>
app/views/fun_urls/index.html.erb:26:in `block in _app_views_fun_urls_index_html_erb___434002087180833655_70161042647500'
app/views/fun_urls/index.html.erb:19:in `each'
app/views/fun_urls/index.html.erb:19:in `_app_views_fun_urls_index_html_erb___434002087180833655_70161042647500'
Rendered /home/puras/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.6ms)
Rendered /home/puras/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
Rendered /home/puras/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (10.4ms)
求赐教~~~