I’m working a very simple forum software to help get my feet wet with ruby on rails. What I am trying to do is add a text area for the post content when a user creates a new topic, but every time I try to add it in the topic form, I get the following error:
NoMethodError in Topics#new
Showing /Users/Ken/dev/forums/app/views/topics/_form.html.erb where line #11 raised:
undefined method `merge' for :content:Symbol
Here’s my new topic form:
<%= form_for @topic do |f| %>
<%= f.error_messages %>
<% if params[:forum] %>
<%= f.hidden_field :forum_id, :value => params[:forum] %>
<% end %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.text_area :post, :content %>
</p>
<p><%= f.submit "Create" %></p>
<% end %>
Here’s my Topic model:
class Topic < ActiveRecord::Base
attr_accessible :name, :last_poster_id, :last_post_at
belongs_to :forum
has_many :posts, :dependent => :destroy
end
Here’s my Post model:
class Post < ActiveRecord::Base
attr_accessible :content
belongs_to :topic
end
How can I get the text area working correctly in the topic form? Do I need to add it to the topic model in order to access it, and if so, how can I do that?
You can use the fields_for helper. See this link, http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for. The first argument could be a Post.new and then you can have the topic text field inside of that block. The end result is a nested form which you can parse in the controller.