新手问题 win7 下练习 rails 想在控制器中用 hash 来显示却无效果。

wack · February 21, 2013 · Last by wack replied at February 21, 2013 · 1804 hits

是这样: 控制器下的 codes:

#encoding: utf-8
    # GET /logs
    # GET /logs.xml
    def index
        log=Log.find(47)
        log.update_attributes([:record_time=>Time.now,:log=> "数据库进行了第二次修改"])
       @logs = Log.all
       respond_to do |format|
           format.html # index.html.erb
           format.xml  { render :xml => @logs }
       end

    end

view 下的 index:

<% @logs.each do |log| %>
  <tr>
    <td><%= log.id %></td>
    <td><%= log.record_time%></td>
    <td><%= log.log %></td>
    <td><%= link_to 'Show', log %></td>
    <td><%= link_to 'Edit', edit_log_path(log) %></td>
    <td><%= link_to 'Destroy',log,:confirm=>"are you sure",:method=>'POST' %></td>
  </tr>

环境:win7 ruby-1.92 rails-3.0 就是 controller 里的:

log.update_attributes([:record_time=>Time.now,:log=> "数据库进行了第二次修改"])

在 view 里的:

<%= log.record_time%> <%= log.log %>

效果出不来 log.update_attributes() 参数转成 update_attributes(:log=>"abc") 这样的话又能显示出这个 abc 来 是什么原因呢?

log.update_attributes({:record_time=>Time.now,:log=> "数据库进行了第二次修改"}),看看这样行不

可以了。thanks a lot

update_attributes需要的是一个 hash,你原来给的参数类型是 array,自然不是你想要的效果 而单写一个参数的时候它会把该参数当成 hash

ps:这跟你用什么操作系统无关的啊。。。(很容易被集火

哦,受教。

You need to Sign in before reply, if you don't have an account, please Sign up first.