【问题标题】:Rails: separate form_for in belongs_to association submitting with unknown format errorRails:在belongs_to关联中单独的form_for以未知格式错误提交
【发布时间】:2017-02-06 19:06:32
【问题描述】:

我有一个 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

【问题讨论】:

    标签: ruby-on-rails forms model-view-controller associations


    【解决方案1】:

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

    无论如何,在我的控制器的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

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多