Rails 4嵌套fields_for不保存

时间:2017-03-26 03:10:32

标签: ruby-on-rails-4 view controller nested-forms

我看到很多帖子,但我不确定它们是否适用于我的问题。主要是因为当我尝试实现这些解决方案时,我得到语法错误。我不知道为什么这似乎是个问题。

我的个人资料对象与用户有1:1的关系。在配置文件的编辑中,我想保存用户的电子邮件。但它不会在这里保存代码和输出...

查看:

<?php

// PROCESSES STUDENT INFO

// get connect page
require '../../connect.php';

// get input info
$student_id = $_POST['student_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$teacher_first_name = $_POST['teacher_first_name'];
$teacher_last_name = $_POST['teacher_last_name'];

// check if input is not empy
if(!empty($student_id) && !empty($first_name) && !empty($last_name) && !empty($teacher_first_name) && !empty($teacher_last_name)) {

    // check if numeric inputs have a number
    if(is_numeric($student_id)) {
        $teacher_check = mysqli_query($link, "SELECT teacher_id FROM teachers WHERE first_name='$teacher_first_name' AND last_name='$teacher_last_name'");

        // check if teacher exists
        if($teacher_check) {
            $row = $teacher_check->fetch_object();

            $result = mysqli_query($link, "INSERT INTO students (student_id, first_name, last_name, teacher_id) VALUES ($student_id, '$first_name','$last_name', $row->teacher_id)");

            if($result) {
                header("Location: ../../../admin.php?message=Success!");
            } else {
                echo mysqli_error($link);
                // header("Location: ../../../admin.php?message=Sorry we ran into an error");
            }
        } else {
            header("Location: ../../../admin.php?message=Teacher Does Not Exist");
        }
    } else {
        header("Location: ../../../admin.php?message=Please add a number for Student ID");
    }

} else if (empty($student_id) || empty($first_name) || empty($last_name)) {
    header("Location: ../../../admin.php?message=Please add you're input values");
}

传递给控制器​​的参数:

              = semantic_form_for @profile do |f|
                = f.inputs class: "form-group" do
                  = f.input :name, input_html: { class: "form-control" }
                  = f.input :username, input_html: { class: "form-control" }
                  = f.semantic_fields_for @profile.user do |u|
                    = u.input :email, input_html: { class: "form-control" }

来自控制器的回应:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"iMkkLX6qN7PagmbnEQlQZ0CH5Q/fl+G9273eHFO9aRk8EG4MisEi3PwQOWocU2wp3UW5ju4DOJcUh5v19pS46A==", "profile"=>{"name"=>"John Doe", "username"=>"JohnnyD", "user"=>{"email"=>"jdoe@email.com"}, "commit"=>"Save", "controller"=>"profiles", "action"=>"update", "id"=>"4"}

白名单:

Unpermitted parameter: user

模特:

params.require(:profile).permit(:name, :username, :user)

我尝试将users_attributes:[:email]添加到所需的参数#syntax错误中。 我尝试将用户:[:email]添加到所需的prarams #syntax错误 我在模型#no效果中尝试使用和不使用accepts_nested_attributes。

有人可以提醒我,因为我显然一无所知。

感谢。

1 个答案:

答案 0 :(得分:0)

我看到两个问题:

首先 - = f.semantic_fields_for @profile.user do |u|应该是:

= f.semantic_fields_for users do |u|

(这里你告诉Rails选择相关的数据库表,所以应该是复数users

第二个强大的参数应该像你已经尝试过的那样:

params.require(:profile).permit(:name, :username, users_attributes: [:email])

希望它能解决您的问题。