为什么我使用嵌套表单在Rails中获得未允许的参数错误?

时间:2014-03-25 22:18:05

标签: ruby-on-rails ruby ruby-on-rails-3 forms

我在这里遇到一个小问题,我试图用嵌套属性创建一个表单,并且我继续获取一个未允许的参数错误。这是user.rb文件中的代码:

class User < ActiveRecord::Base
    has_many :topics

    has_many :subjects, through: :topics
end

这是subject.rb文件中的代码:

class Subject < ActiveRecord::Base
    has_many :topics
    has_many :users, through: :topics, dependent: :destroy
    accepts_nested_attributes_for :topics, reject_if: :all_blank, update_only: true,allow_destroy: true
    validates :name, presence: true
end

这是topic.rb文件中的代码:

class Topic < ActiveRecord::Base
    belongs_to :subject
    belongs_to :user
    validates :name, presence: true
end

这是我subjects_controller.rb中的代码:

class SubjectsController < ApplicationController
    before_filter :require_login

    def new
        @subject = current_user.subjects.build
        @subject.topics.build
    end

    def create
        @subject = current_user.subjects.build(subject_params)
        if @subject.save
            flash[:success] = "New subject created!"
            redirect_to user_path(@user)
        else
            flash[:error] = "Errors!!!"
            render :new
        end
    end

    private
    def subject_params
        params.require(:subject).permit(:name, topics_attributes: [:name,:user_id,:subject_id])
    end

end

这是我在视图中创建表单的代码:

<%= form_for @subject do |f| %>

    <%= f.label :name, "Subject" %><br>
    <%= collection_select :user, :subject_id, Subject.all, :id, :name %>

    <%= f.fields_for :topic do |t| %>

        <%= t.label :name, "Topic" %>
        <%= t.text_field :name %>

    <% end %>

    <div class="text-center"><%= f.submit class: "button radius" %></div>

<% end %>

这是我的topics_controller.rb

class TopicsController < ApplicationController
    before_filter :require_login
    before_action :find_subject

    def new
        @topic = @subject.topics.build
    end

    def create
        @topic = @subject.topics.build(topic_params)
        @topic.user_id = current_user.id
        if @topic.save
            flash[:success] = "Success!"
        else
            flash[:error] = "Error!"
        end
    end

    private
    def topic_params
        params.require(:topic).permit(:name,:subject_id,:user_id)
    end

    def find_subject
        @subject = Subject.find(params[:subject_id])
    end
end

我在想我收到Unpermitted parameters: topic错误的原因是因为我在提交表单时没有传递“user_id”或“subject_id”。如果是这种情况,我将如何使用隐藏的表单字段执行此操作?或者是否有人对我可能出错的地方有任何其他建议?

这是当前正在发生的事情的服务器日志:

Processing by SubjectsController#create as HTML
Processing by SubjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"o42ESf7kQ4R3pB6CUvcEetmqoZOmIK8V4UrVTQ5BW/M=", "user"=>{"subject_id"=>"1"}, "subject"=>{"topics_attributes"=>{"0"=>{"name"=>"something"}}}, "commit"=>"Create Subject"}
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"o42ESf7kQ4R3pB6CUvcEetmqoZOmIK8V4UrVTQ5BW/M=", "user"=>{"subject_id"=>"1"}, "subject"=>{"topics_attributes"=>{"0"=>{"name"=>"something"}}}, "commit"=>"Create Subject"}
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
   (0.2ms)  BEGIN
   (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
   (0.3ms)  ROLLBACK

2 个答案:

答案 0 :(得分:1)

更新视图中的fields_for,如下所示:

<%= f.fields_for :topics do |t| %>

    <%= t.label :name, "Topic" %>
    <%= t.text_field :name %>

<% end %>

由于您在One to ManySubject之间设置了Topic关系,因此必须使用复数topics作为fields_for参数。

答案 1 :(得分:0)

尝试从

更改Subject
has_many :subjects, through: :topics, dependent: :destroy

has_many :users, through: :topics