开发者

Rails problem display attribute key along with attributes value

I have the following problem. I have a form which takes input for a "Chart" object. But after processing the form, i wish to display one of the values, and it adds the key of this value.

Class model

class Chart 

attr_accessor :title, :series

  def initialize(title = nil, series = [])
      @title, @series = title, series
  end
end 

View of form:

<% form_for :chart , :url => { :action => "show" } do |f| %>

<p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
</p>...
<% end %>

Chart controller, show method:

def show

    @chart = Chart.new(params[:chart])
end

View of show:

<h2><%=h @chart.title %></h2>

Which displays: "title"input_forms_title""

fo开发者_运维问答r example: writing in the input form: Economy, prints in the show view: "titleEconomy"

Any ideas?


I have just figured it out. The problem was in the constructor or initialize method. By changing the initialize method to:

def initialize( options = {} )
    @title = options[:title]
    @series = []
end

It now accepts all params perfectly!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜