I know this question should be really simple, but for some reason thoroughly understanding Forms has alluded me over the years.
Basically, I want to create a new ‘feedback’ – which can be thought of as similar to a Tweet.
So when you are entering the text – which can only be done when you are logged in, (i.e. current_user is set), it should automatically store the poster_id and the receiver_id.
Here is my Feedback model:
class Feedback < ActiveRecord::Base
belongs_to :poster, :class_name => 'User'
belongs_to :receiver, :class_name => 'User'
end
# == Schema Information
#
# Table name: feedbacks
#
# id :integer not null, primary key
# poster_id :integer
# receiver_id :integer
# content :string(255)
# created_at :datetime
# updated_at :datetime
This is my _form partial for the creation of new Feedback objects/records.
<%= form_for(@feedback) do |f| %>
<% if @feedback.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@feedback.errors.count, "error") %> prohibited this feedback from being saved:</h2>
<ul>
<% @feedback.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="poster_id">
<%= f.hidden_field(:poster, current_user) %><br />
</div>
<div class="receiver_id">
<%= f.hidden_field(:receiver, @user) %><br />
</div>
<div class="field">
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I tried this, but the error I keep getting is:
undefined method 'merge' for nil:NilClass
I am pretty sure I am not doing this right, but just based on the API docs and the guide, I can’t quite figure out how to do it right and why.
So if you could tell me not only what the solution is, but also why that is the case, I would really appreciate it.
Thanks.
Actually, I am not really sure about @user. Maybe you want @user.id or something?