1

我是 ROR 的新手,但很快就赶上了。我已经在这个问题上工作了几个小时,这似乎是一个错误。我没有任何意义。

我有一个具有以下迁移的数据库:

class CreateWebsites < ActiveRecord::Migration
  def self.up
    create_table :websites do |t|
      t.string :name
      t.integer :estimated_value
      t.string :webhost
      t.string :purpose
      t.string :description
      t.string :tagline
      t.string :url      
      t.integer :adsense
      t.integer :tradedoubler
      t.integer :affiliator
      t.integer :adsense_cpm
      t.boolean :released
      t.string :empire_type

      t.string :oldid
      t.string :old_outlink_policy
      t.string :old_inlink_policy
      t.string :old_priority
      t.string :old_profitability



      t.integer :priority_id
      t.integer :project_id
      t.integer :outlink_policy_id
      t.integer :inlink_policy_id

      t.timestamps
    end
  end

  def self.down
    drop_table :websites
  end
end

我已经根据此迁移验证了在数据库中创建的也是整数、字符串等。

通过脚手架生成控制器后我没有接触过控制器,即它是带有显示,索引等的标准控制器。

现在。当我通过 Web 表单、Rails 控制台或直接在数据库中输入数据时——例如用于url的 www.domain.com或用于adsense的 500——它将毫无问题地在数据库中创建。

然而,当它在网站上发布时,变量完全疯了。Adsense(整数)变成日期,url(字符串)变成浮点数,以此类推。这只发生在少数变量上。

这也会产生“参数超出范围”的问题,因为我输入 500,Rails 将尝试将其输出为 date => crash 和“参数超出范围”。

那么,我该如何解决/解决这个问题?为什么格式会发生变化?可能是因为控制器中的 respond_to 吗?

干杯,

克里斯托弗

控制器:

class WebsitesController < ApplicationController
  # GET /websites
  # GET /websites.xml
  def index
    @websites = Website.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @websites }
    end
  end

  # GET /websites/1
  # GET /websites/1.xml
  def show
    @website = Website.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @website }
    end
  end

  # GET /websites/new
  # GET /websites/new.xml
  def new
    @website = Website.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @website }
    end
  end

  # GET /websites/1/edit
  def edit
    @website = Website.find(params[:id])
  end

  # POST /websites
  # POST /websites.xml
  def create
    @website = Website.new(params[:website])

    respond_to do |format|
      if @website.save
        format.html { redirect_to(@website, :notice => 'Website was successfully created.') }
        format.xml  { render :xml => @website, :status => :created, :location => @website }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @website.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /websites/1
  # PUT /websites/1.xml
  def update
    @website = Website.find(params[:id])

    respond_to do |format|
      if @website.update_attributes(params[:website])
        format.html { redirect_to(@website, :notice => 'Website was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @website.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /websites/1
  # DELETE /websites/1.xml
  def destroy
    @website = Website.find(params[:id])
    @website.destroy

    respond_to do |format|
      format.html { redirect_to(websites_url) }
      format.xml  { head :ok }
    end
  end
end

看法:

<h1>Listing websites</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Estimated value</th>
    <th>Webhost</th>
    <th>Purpose</th>
    <th>Description</th>
    <th>Mail text</th>
    <th>Adsense last year</th>
    <th>Tradedoubler last year</th>
    <th>Affiliator last year</th>
    <th>Adsense cpm</th>
    <th>Tagline</th>
    <th>Url</th>
    <th>Released</th>
    <th>Empire type</th>
    <th>Priority</th>
    <th>Project</th>
    <th>Outlink policy</th>
    <th>Inlink policy</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @websites.each do |website| %>
  <tr>
    <td><%= website.name %></td>
    <td><%= website.estimated_value %></td>
    <td><%= website.webhost %></td>
    <td><%= website.purpose %></td>
    <td><%= website.description %></td>
    <td><%= website.adsense %></td>
    <td><%= website.tradedoubler %></td>
    <td><%= website.affiliator %></td>
    <td><%= website.adsense_cpm %></td>
    <td><%= website.tagline %></td>
    <td><%= website.url %></td>
    <td><%= website.released %></td>
    <td><%= website.empire_type %></td>
    <td><%= website.priority_id %></td>
    <td><%= website.project_id %></td>
    <td><%= website.outlink_policy_id %></td>
    <td><%= website.inlink_policy_id %></td>
    <td><%= link_to 'Show', website %></td>
    <td><%= link_to 'Edit', edit_website_path(website) %></td>
    <td><%= link_to 'Destroy', website, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Website', new_website_path %>

输出:

Name: Example.com

Estimated value: 1000

Webhost: Host.com

Purpose: Yada

Description: Yada yada

Adsense last year: 946684824

Tradedoubler last year: 0

Affiliator last year: 946684824

Adsense cpm:

Tagline:

Url: 0.0

Released: false

Empire type: 0

Priority:

Project:

Outlink policy:

Inlink policy: 

对于这个输出,我使用了以下输入:

Name: Example.com
Estimated value: 1000
Webhost: Host.com
Purpose: Yada
Desc: Yada yada
Adsense last year: 0
Tradedoubler last year: 0
Affiliator last year: 0
...
The rest of the fields I left blank

请注意,例如,该 url 留空(并创建了 0.0 输出)。帝国类型留空并创建 0 输出。

为了进一步澄清,数据库中的数据正是我的输入。只有输出错误。

当我回到编辑模式时。“Adsense”和“Affiliator”的输出(默认值)显示2000-01-01 00:00:24 UTC

4

1 回答 1

0

您的表格标题与单元格不匹配。例如,您的第 6 个<th>是“邮件文本”,但相关显示的值<td>website.adsense,依此类推。

于 2011-02-20T09:55:18.027 回答