Rails collection_select不保存 [英] Rails collection_select not saving

查看:83
本文介绍了Rails collection_select不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的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.




  1. 模型

  2. 输入

  3. 收集用户

  4. 将保存什么

  5. 显示

  6. 提示与请选择

  1. The model
  2. input
  3. collect of users
  4. what will be saved
  5. what is displayed
  6. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆