Rails collection_select不保存 [英] Rails collection_select not saving
问题描述
我有一个非常简单的rails应用程序来提问。三个模型问题,用户,老师。问题属于用户和教师。用户和教师has_many问题。
I have a very simple rails app to ask questions. Three Models Question, User, Teacher. Questions belong_to User and Teacher. User and Teacher has_many Questions.
我试图让用户和老师下载选择问题_form.html.erb文件。
I am trying to have a drop down of users and teachers to select in the Questions _form.html.erb file.
我有这个collection_select,它显示正常,但不会在问题user_id字段中保存用户ID。
I have this collection_select and it displays fine but it will not save user id in the Questions user_id field.
<%= collection_select(:question, :user_id, User.all, :id, :name, {:prompt=>true}) %>
1. 2. 3. 4. 5. 6.
- 模型
- 输入
- 收集用户
- 将保存什么
- 显示
- 提示与请选择
- The model
- input
- collect of users
- what will be saved
- what is displayed
- prompt with "please select"
请告诉我我在做什么错,将user.id保存到question.user_id
Please tell me what I am doing wrong to save the user.id to the question.user_id
推荐答案
一切似乎都符合你的看法。您的问题可能在控制器或型号中,具体取决于您的Rails版本。
Everything seems fine with your view. Your problem could be in controller or model, depending on your Rails version.
在Rails 3:
class Question < ActiveRecord::Base
attr_accessible :user_id
在Rails 4中:
class QuestionsController < ApplicationController
def question_params
params.require(:question).permit(:user_id)
end
第二个猜测
您发布的路由不正确。
Second guess
You are posting to an incorrect route.
这篇关于Rails collection_select不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!