开发者

Controller changes format on variables when publishing

开发者 https://www.devze.com 2023-02-12 07:31 出处:网络
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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号