初入rails的折腾

想必大家都有这样一种感受,初次接触某些东西的时候,要折腾一番,只不过折腾有大小罢了。
就此,说说rails折腾我:

按照以往的习惯,开发环境准备好了之后,按照实例做,然后再理解,rails真是折腾我啊, 按照《应用ruby进行web敏捷开发》的实例做了一下,发现有好几个地方和书里描述的不一样,应该是版本的问题,俺的rails是2.0.2,所以在网上搜索一番之后,解决了书中demo几处问题(严格来说,不说书中的错误,是因为版本造成的问题),这些问题是相继出现的,好像西游记一样,一关一关等着你:

1.render_text ,   改为 :render :text =>        这个比较容易修改。
2.找不到scaffold方法, ruby script/plugin install scaffolding
3.undefined method `paginate' for #<AdminController:0x301f0c4>, ruby script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination
4.为 %工程名%/vendor/plugins/scaffolding/lib/templates 增添一下文件:

# new.erb
<h1>New <%= @scaffold_singular_name %></h1>

<%= error_messages_for(@scaffold_singular_name) %>
<%= form(@scaffold_singular_name, :action => "create#{@scaffold_suffix}"%>

<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>

#list.erb
<h1>Listing <%= @scaffold_plural_name %></h1>

<table>
  
<tr>
  
<% for column in @scaffold_class.content_columns %>
    
<th><%= column.human_name %></th>
  
<% end %>
  
</tr>

<% for entry in instance_variable_get("@#{@scaffold_plural_name}"%>
  
<tr>
  
<% for column in @scaffold_class.content_columns %>
    
<td><%= entry.send(column.name) %></td>
  
<% end %>
    
<td><%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => entry %></td>
    
<td><%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => entry %></td>
    
<td><%= link_to "Destroy", {:action => "destroy#{@scaffold_suffix}", :id => entry}, { :confirm => "Are you sure?", :method => :post } %></td>
  
</tr>
<% end %>
</table>

<%= link_to "Previous page", { :page => instance_variable_get("@#{@scaffold_singular_name}_pages").current.previous } if instance_variable_get("@#{@scaffold_singular_name}_pages").current.previous %>
<%= link_to "Next page", { :page => instance_variable_get("@#{@scaffold_singular_name}_pages").current.next } if instance_variable_get("@#{@scaffold_singular_name}_pages").current.next %>

<br />

<%= link_to "New #{@scaffold_singular_name}", :action => "new#{@scaffold_suffix}" %>

#show.erb
<% for column in @scaffold_class.content_columns %>
  
<p>
    
<b><%= column.human_name %>:</b>
    
<%= instance_variable_get("@#{@scaffold_singular_name}").send(column.name) %>
  
</p>
<% end %>

<%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}"%> |
<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>

#edit.erb
<h1>Editing <%= @scaffold_singular_name %></h1>

<%= error_messages_for(@scaffold_singular_name) %>
<%= form(@scaffold_singular_name, :action => "update#{@scaffold_suffix}"%>

<%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}"%> |
<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>

#layout.erb
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  
<title>Scaffolding</title>
  
<style>
    body { background
-color: #fff; color: #333; }

    body, p, ol, ul, td {
      font
-family: verdana, arial, helvetica, sans-serif;
      font
-size:   13px;
      line
-height: 18px;
    }

    pre {
      background
-color: #eee;
      padding: 10px;
      font
-size: 11px;
    }

    a { color: 
#000; }
    a:visited { color: #666; }
    a:hover { color: #fff; background-color:#000; }

    .fieldWithErrors {
      padding: 2px;
      background
-color: red;
      display: table;
    }

    
#errorExplanation {
      width: 400px;
      border: 2px solid red;
      padding: 7px;
      padding
-bottom: 12px;
      margin
-bottom: 20px;
      background
-color: #f0f0f0;
    }

    
#errorExplanation h2 {
      text-align: left;
      font
-weight: bold;
      padding: 5px 5px 5px 15px;
      font
-size: 12px;
      margin: 
-7px;
      background
-color: #c00;
      color: #fff;
    }

    
#errorExplanation p {
      color: #333;
      margin-bottom: 0;
      padding: 5px;
    }

    
#errorExplanation ul li {
      font-size: 12px;
      list
-style: square;
    }
  
</style>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield %>

</body>
</html>

5.ActionController::InvalidAuthenticityToken , 在控制器类中加入
#===========================================
    scaffold :book
    # uses the cookie session store (then you don't need a separate :secret)
    protect_from_forgery :except => :index

    # uses one of the other session stores that uses a session_id value.
    protect_from_forgery :secret => 'my-little-pony', :except => :index

    # you can disable csrf protection on controller-by-controller basis:
    skip_before_filter :verify_authenticity_token

经过上述一番折腾,就ok了,crud全部实现了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值