Controller在发布时更改变量的格式

时间:2011-02-20 07:21:49

标签: ruby-on-rails format respond-to

我是ROR的新手但很快就赶上了。我一直在研究这个问题几个小时,这似乎是一个bug。我没有任何意义。

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

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

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

我通过脚手架生成它后没有触及控制器,即它是带有show,index等的标准控制器。

现在。当我通过Web表单,rails控制台或直接在数据库中输入数据到数据库时 - 例如www.dmain.com url 或500 adsense - 它将在数据库中创建,没有问题。

但是,当它在网站上发布时,变量就完全疯了。 Adsense(整数)变成日期,url(字符串)变成浮点数,依此类推。这只发生在一些变量上。

这也会产生“参数超出范围”的问题,因为我输入500并且Rails会尝试将其输出为date =&gt;崩溃和“争论超出范围”。

那么,我该如何解决这个问题?为什么格式会改变?可能是因为控制器中的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

1 个答案:

答案 0 :(得分:0)

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