我是Ruby和Rails的新手,所以很抱歉,如果這看起來太noob。 我創建了一個名爲stream的資源,另一個資源稱爲tasks,並且已經使用has_many和belong_to正確映射了它們。一切正常,直到我決定增加一個對我的看法Stream.show「快速任務添加表」:調用多個參數的方法的問題
這裏是表單視圖代碼:
<%= 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 %>
這裏是我的Stream.show行動:
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
這是我的任務控制器:
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
看起來很基本的給我。問題是當它執行tasks.create時,我收到以下錯誤消息:「驗證失敗:用戶不能爲空,所有者不能爲空」
我在做什麼錯?
編輯:從評論添加模型代碼
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