回报率;对象期望得到字符串

时间:2015-06-18 18:28:08

标签: ruby-on-rails ruby

我发现了几个相关的主题,但无法弄清楚如何解决这个问题,

这是我的代码

 <%= f.label :projeto %><br>

 <%= f.collection_select :projeto, Projeto.order(:id), :id, :name, include_blank: true %>

model => task belongs_to projeto and projeto has_many tasks 

完全错误:

ActiveRecord::AssociationTypeMismatch at /tasks/1

Projeto(#69996814678740) expected, got String(#69996762580840)

数据库设置为t.references。

Task_controller

def update
  respond_to do |format|
    if @task.update(task_params)
      format.html { redirect_to @task, notice: 'Task was successfully updated.' }
      format.json { render :show, status: :ok, location: @task }
    else
      format.html { render :edit }
      format.json { render json: @task.errors, status: :unprocessable_entity }
    end
  end
end

def task_params
  params.require(:task).permit(:seq, :descr, :seqpai, :typo, :hour, :projeto)
end


Started PATCH "/tasks/1" for 127.0.0.1 at 2015-06-18 14:30:23 -0300
Processing by TasksController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Is7YTC0v5OONEEsIgOvmI+CEuVYG/WsoKWzskGippD2eOwthKVHb2dI+S19GkkI9aU0IwTrzwERlLq2ybWbGxw==", "task"=>{"seq"=>"0", "descr"=>"Projeto", "seqpai"=>"", "typo"=>"Analitica", "hour"=>"12", "projeto"=>"1"}, "commit"=>"Update Task", "id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Task Load (0.2ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 25ms (ActiveRecord: 0.8ms)

2 个答案:

答案 0 :(得分:0)

通常,这是通过为期望模型关联的事物分配字符串引起的。例如:

# Not allowed, `project=` expects a Project model
@task.project = params[:project_id]

# Converts parameter into model, but may throw exception
@task.project = Project.find(params[:project_id])

# Direct assignment, requires validation on model level
@task.project_id = params[:project_id]

答案 1 :(得分:0)

我更改了View for     &lt;%= f.label:projeto_id%&gt;

 

并在控制器projeto_id

中存在

然后工作很好,

谢谢大家的提示。