无法编辑(更新嵌套模型轨道4

时间:2014-05-13 13:07:32

标签: ruby-on-rails forms ruby-on-rails-4 nested-attributes

我在rails 4中构建嵌套表单。我按照预期工作但是当我尝试编辑页面时,它只在这里编辑父元素project.Child模型问题元素甚至不在编辑模式下显示

我的_form.html.erb为

    <%= nested_form_for (@project) do |f| %>
    <% if @project.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

    <ul>
    <% @project.errors.full_messages.each do |message| %>
      <li><%= message %></li>
     <% end %>
     </ul>
     </div>
    <% end %>

    <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
    </div>

    <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :f => builder %>
                    <p><%= f.link_to_add "Add a questions",:questions %></p>
    <% end %>

    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>

_question.html.erb

      <p>
            <%= f.label :content, "Question" %><br />
            <%= f.text_area :content, :rows => 3 %><br />
            <%= f.label :subject, "Question" %><br />
            <%= f.text_field :subject %><br />
            <%= f.link_to_remove "Remove this task" %>

     </p>

project.rb

       has_many :questions, :dependent => :destroy

       accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

question.rb

      class Question < ActiveRecord::Base

       belongs_to :project

       end

项目控制器

      def new
      @project = Project.new
       3.times do
       question = @project.questions.build
        end
      def project_params
      params.require(:project).permit(:name,questions_attributes: [:id,:project_id,:subject,:content])

      end

        def create
        @project = Project.new(project_params)

        respond_to do |format|
        if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
         format.json { render :show, status: :created, location: @project }
        else
         format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
       end
      end

我使用了“ nested_form ”gem 请帮我摆脱这个错误

问题控制器def: -

      def question_params
      params.require(:question).permit(:id, :project_id, :subject, :content)
      end

projects_controller

     before_action :set_project, only: [:show, :edit, :update, :destroy]


  def edit
   @question = Question.find_by_project_id(params[:id])
  end

    # POST /projects
    # POST /projects.json
    def create
    @project = Project.new(project_params)

     respond_to do |format|
     if @project.save
       format.html { redirect_to @project, notice: 'Project was successfully created.' }
       format.json { render :show, status: :created, location: @project }
     else
       format.html { render :new }
       format.json { render json: @project.errors, status: :unprocessable_entity }
      end
     end
    end

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

私有

# Use callbacks to share common setup or constraints between actions.

      def set_project
       @project = Project.find(params[:id])
    end

# Never trust parameters from the scary internet, only allow the white list through.
def project_params
 params.require(:project).permit(:name,questions_attributes: [:id,:project_id,:subject,:content,:_destroy])
 #params.require(:project).permit(:name, questions_attributes: [:id,:project_id,:subject,:content, :_destroy])

end

0 个答案:

没有答案