Rails: 在 belongs_to 关联中分隔 form_for 提交未知格式错误

Rails: separate form_for in belongs_to association submitting with unknown format error

我有has_many/belongs_to关系,Costs/Cost_Dependencies:

用户创建成本,它重定向到索引按钮。

用户点击 + 按钮添加 Cost_dependency -> 发送到 /costs/:id/cost_dependencies/new

呈现此表单:

<%= form_for([@cost, @cost_dependency], :html => {:class => "form-group"}) do |f| %>

  <div class="row">
    <div class="col-xs-10 col-xs-offset-1">
      <div class="row center-block">
        <div class="col-xs-3 center-block">
          <%= f.label :dependency_category, "Dependecy Category" %>
          <%= f.select :dependency_category, options_for_select(getAllCategories, :selected => @cost_dependency.dependency_category.nil? ? 'Please Select' : @cost_dependency.dependency_category), {}, {:required => true, :class => 'form-control', id: "category-select"} %>
          <div id="new-category">
            <br>
            <%= f.label :dependency_category, "New Dependency Category Name" %>
            <%= f.text_field :dependency_category, :class => 'form-control', placeholder: "e.g. Size", id: 'new-category-text'%>
          </div>
        </div>
        <div class="col-xs-3 ">
          <%= f.label :dependency_option, "New Option Name" %>
          <%= f.text_field :dependency_option, :class => 'form-control', placeholder: "e.g. 8.5\" x 11\"", id: 'new-option-text'%>
        </div>
        <div class="col-xs-3">
          <%= f.label :per_job, "Cost per job" %>
          <div class="input-group">
            <span class="input-group-addon">$</span>
            <%= f.number_field :per_job, :class => 'form-control', :step => 0.01 %>
          </div>
        </div>
        <div class="col-xs-3" >
          <%= f.label :per_page, "Cost per page" %>
          <div class="input-group">
            <span class="input-group-addon">$</span>
            <%= f.number_field :per_page, :class => 'form-control', :step => 0.01 %>
          </div>
        </div>
      </div>
      <br>
      <div>
        <%= f.submit (@edit)? 'Update Cost Dependency':'Submit Cost Dependency', :class => 'btn btn-info btn-lg' %>
      </div>
    </div>
  </div>
<% end %>

但是当他们单击提交按钮时,日志显示 rollback transaction,然后我被带到 costs/:id/cost_dependencies,我不知道为什么它会把我带到那里。

cost_dependencies 的控制器:

def new
    @cost = Cost.find(params[:cost_id])
    @cost_dependency = CostDependency.new
    respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @cost_depencency }
    end
end

def index

end

def create
    @cost_dependency = CostDependency.new(cost_dependencies_params)
    respond_to do |format|
    if @cost_dependency.save
      format.html { redirect_to controller: 'costs', action: 'index', status: 303, notice: [true, 'Cost Dependency was successfully created.'] }
      format.json { render json: @cost_dependency, status: :created, location: @cost }
    #else
      #format.html { render action: "new" }
      #format.json { render json: @cost_dependency.errors, status: :unprocessable_entity }
      # this commented out is what allows me to see the unknown format error
    end
  end
end

private
def cost_dependencies_params
    params.require(:cost_dependency).permit(:cost_id, :dependency_category, :dependency_option, :per_job, :per_page)
end

好的,我知道出了什么问题。微妙的事情可能只是一种正确的痛苦...

无论如何,在我的控制器的 create 操作中,我需要这个而不是我拥有的:

def create
    @cost = Cost.find(params[:cost_id])
    @cost_dependency = @cost.cost_dependencies.new(cost_dependencies_params)
    respond_to do |format|
    if @cost_dependency.save
      format.html { redirect_to controller: 'costs', action: 'index', status: 303, notice: [true, 'Cost Dependency was successfully created.'] }
      format.json { render json: @cost_dependency, status: :created, location: @cost }
    #else
      #format.html { render action: "new" }
      #format.json { render json: @cost_dependency.errors, status: :unprocessable_entity }
    end
  end
end

我添加了类似于新动作的@cost,而不是CostDependency.new我不得不经历成本@cost.cost_dependencies.new