控制器在发布时更改变量的格式

发布于 2024-10-18 01:03:15 字数 6512 浏览 8 评论 0原文

我是 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 =>;崩溃和“参数超出范围”。

那么,我该如何解决/排除这个问题?为什么格式会改变?难道是因为控制器中的respond_to的原因?

干杯,

Christoffer

控制器:

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 输出)。 Empire 类型留空并创建 0 输出。

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

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

I am a newbie to ROR but catching on quickly. I have been working on this problem for a couple of hours now and it seems like a bug. I does not make any sense.

I have a database with the following migration:

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

I have verified that what is created in the database also is integers, strings etc according to this migration.

I have not touched the controller after generating it through scaffold, i.e. it is the standard controller with show, index etc.

Now. When I enter data into the database, either through the web form, in rails console or directly in the database - such as www.domain.com for url or 500 for adsense - it will be created in the db without problem.

However, when it is being published on the website the variables go completely nuts. Adsense (integer) turns into date, url (string) turns into a float, and so on. This only happens to a few of the variables.

This will also create a problem with "argument out of range" since I input 500 and Rails will try to output it as date => crash and "argument out of range".

So, how do I fix/trouble shoot this? Why do the formats change? Could it be because of the respond_to in the controller?

Cheers,

Christoffer

Controller:

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

View:

<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 %>

Output:

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: 

For this output I used the following input:

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

Note, for example, that url was left blank (and created a 0.0 output). Empire type was left blank and created a 0 output.

To further clarify, the data in the database is EXACTLY as my input. It is only the output that is wrong.

When I go back to edit mode. The output (default value) for "Adsense" and "Affiliator" shows 2000-01-01 00:00:24 UTC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-10-25 01:03:15

您的表格标题与单元格不匹配。例如,您的第 6 个 是“M​​ail text”,但相关 中显示的值是 website.adsense , 等等。

Your table headers don't match the cells. For example your 6th <th> is "Mail text", but the value displayed in the related <td> is website.adsense, and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文