I am new to Ruby and Rails so sorry if this looks too noob. I have created a resource called stream and another resource called tasks and have mapped them properly using has_many and belong_to. Everything works until I decided to add a "Quick Task Add form" on my Stream.show view:
Here is the view code for the form:
<%= form_for(@task) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_field :title %> <%= f.submit "Add Task" %>
<%= hidden_field_tag(:stream_id, @stream.id) %>
</div>
<% end %>
Here is my Stream.show action:
def show
@stream = Stream.find(params[:id])
@user = User.find(@stream.user_id)
@tasks = @stream.tasks.paginate(:page => params[:page])
@title = @stream.title
@task = Task.new
end
And here is my task controller:
class TasksController < ApplicationController
def create
@stream = Stream.find(params[:stream_id])
@stream.tasks.create!({:title => params[:task][:title], :user_id => 1, :owner => 1})
if @stream.save
flash[:success] = "Task created succesfully!"
else
flash[:error] = "Error creating task"
end
redirect_to @stream
end
end
Looks pretty basic to me. The problem is when it executes tasks.create, I get the following error message: "Validation failed: User can't be blank, Owner can't be blank"
What am I doing wrong?
edit: adding model code from comment
class Stream < ActiveRecord::Base attr_accessible :title belongs_to :user has_many :tasks, :dependent => :destroy validates :title, :presence=> true, :length => { :maximum =>50 } validates :user_id, :presence => true end class Task < ActiveRecord::Base attr_accessible :title belongs_to :stream validates :title, :presence=> true, :length => { :maximum =>70 } validates :user_id, :presence => true validates :owner, :presence => true validates :stream_id, :presence => true default_scope :order => "updated_at" end