在Ruby on rails上为每条记录创建一个输入日志表单

时间:2014-08-06 14:14:11

标签: ruby-on-rails ruby forms

早上好,

我对如何在rails上使用ruby创建日志文件感到困惑。我能在php中很容易地做到这一点。但现在我正在学习Ruby On Rails

我的设置:

CentOS 6.5
Ruby 1.9.3
Rails 3.0
Postgres 9.3

我想要做的是在禁用的表单上有一个文本区域。但是从数据库中获取数据。保存到数据库中的:text字段中。

然后我想在表单上添加一个字符串字段,它将为New Notes添加一个时间戳,并将其添加到禁用字段中的信息。

每次加载此表单时,我都不需要或想要更新文本字段的额外列我希望该字段为空白。

类似于以下示例:

enter image description here

这是我拥有的一个例子

查看:

<%= form_for(@lead) do |f| %>
    <% if @lead.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@lead.errors.count, "error") %> prohibited this lead from being saved:</h2>

          <ul>
            <% @lead.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>

          </ul>
        </div>
    <% end %>
    <div align="center" class="edit_view" id="<%= @lead.id %>">
<table width="50%" border="0" cellpadding="0">
  <tr>
    <th scope="col"><%= f.label :notes %></th>
    <th scope="col"><%= f.text_area :notes %></th>
  </tr>
  <tr>
    <th scope="row"><%= f.label :update %></th>
    <th scope="row"><%= f.text_area :update %></th>
  </tr>
  <tr>
    <th colspan="2" scope="row"><%= f.submit %></th>
  </tr>
</table>
</div>

控制器

  def edit
    @lead = Lead.find(params[:id])
  end

  def update
    @lead = Lead.find(params[:id])

    respond_to do |format|
      if @lead.update_attributes(params[:lead])
        if @lead.save
          flash[:notice] = "Lead was successfully updated."
        end
        format.html { render action: "edit"  }

        #format.html { redirect_to @lead, notice: 'Lead was successfully updated.' }
        #format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @lead.errors, status: :unprocessable_entity }
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

我能够做到这一点:

  def enter_notes
    time = Chronic.parse('now').strftime('%m-%d-%Y %l:%M %p')
    oldnotes = @lead.notes
    params[:lead][:notes] = "#{time} #{params[:lead][:note1]} \n\n#{oldnotes}"
    params[:lead].delete(:note1)
  end

  def update
    @lead = Lead.find(params[:id])
    enter_notes
    if @lead.update_attributes(params[:lead])
      flash[:notice] = "Lead was successfully updated."
      if @lead.updated?
        redirect_to edit_lead_path(@leads[@index])
      else
        render action: "edit"
      end
    else
       render action: "edit"
    end

  end