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

By : Avir94
Source: Stackoverflow.com
Question!

I have a has_many/belongs_to relationship, Costs/Cost_Dependencies:

User creates Cost, it redirects to index button.

user clicks + button to add a Cost_dependency -> sends to /costs/:id/cost_dependencies/new

renders this form:

<%= 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 %>

but when they click submit button, logs shows a rollback transaction, I then get taken to the costs/:id/cost_dependencies which I don't know why it's taking me there.

controller for 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
By : Avir94


Answers

Okay, I figured out what was wrong. Subtle things can just be a right pain...

anyways, in my create action of my controller, I needed this instead of what I had:

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

I added the @cost similar to the new action, and instead of CostDependency.new I had to go through the cost @cost.cost_dependencies.new

By : Avir94


It appears you want element-wise multiplication. That is accomplished with the *-operator. If these were in a list (avoiding the c() operation which created one big vector and loss all dimensions) then you can use Reduce:

> l1 <- list( mat1, mat2, mat3)
> 
> Reduce("*", l1)
     [,1] [,2] [,3] [,4]
[1,]    1    8   27   64
[2,]  125  216  343  512
[3,]  729 1000 1331 1728
[4,] 2197 2744 3375 4096

If you wnated iot as a vector you could then use c:

 c( Reduce("*", l1) )
 [1]    1  125  729 2197    8  216 1000 2744   27  343 1331 3375   64  512 1728
[16] 4096

If as suggested you wanted matrix multiplication, then perhaps this (with my list of your matrices):

 c( Reduce("%*%", l1) )

 [1]  3140  7268 11396 15524  3560  8232 12904 17576  3980  9196 14412 19628  4400
 [14] 10160 15920 21680
By : 42-


Here is what you want in terms of matrix multiplication

mat1 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))
mat2 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))
mat3 <- as.list(matrix(c(1:16), 4, 4, byrow = TRUE))

l1 <- list(mat1, mat2, mat3)
l2 <- list(mat1, mat2, mat3)
l3 <- list(mat1, mat2, mat3)

matrix(unlist(l1[[1]]),4,4) %*% matrix(unlist(l2[[1]]),4,4) %*% matrix(unlist(l3[[1]]),4,4)
matrix(unlist(l1[[2]]),4,4) %*% matrix(unlist(l2[[2]]),4,4) %*% matrix(unlist(l3[[2]]),4,4)
matrix(unlist(l1[[3]]),4,4) %*% matrix(unlist(l2[[3]]),4,4) %*% matrix(unlist(l3[[3]]),4,4)
By : Stephen


This video can help you solving your question :)
By: admin