Reputation: 189
Trying to make sub categories of categories in nested resources. The issue that I have is that it doesn't seem to work. All i get are undefined method
sub_category_path' for #<#:0x007f8fcde33550>
Did you mean? new_category_pathswell as
first value of a form cannot be nil`.
My controller methods are as follows:
SubCategory Update:
@category = @subcategory.category.id
if @subcategory.update(subcategory_params)
flash[:success] = "Subcat name updated"
redirect_to category_sub_category_path(@subcategory)
else
render 'edit'
end
SubCategory new:
@category = Category.find(params[:category_id]) # TODO:subcategory.category.id?
@category_sub = @category.sub_categories.new
#@subcategory = @category.sub_categories.new
SubCategory create:
@subcategory = SubCategory.new(subcategory_params)
if @subcategory.save
flash[:success] = "subcategory created"
redirect_to category_sub_category_path
else
flash[:error] = "subcategory failed"
render 'new'
end
And my private methods:
private
def set_sub_category
@subcategory = SubCategory.find(params[:id])
end
def subcategory_params
params.require(:sub_category).require(:name, :category_id)
end
end
I don't have a SubCategories index, the viewing of the SubCategory is based off clicking a link in Categories index.html.erb
which can be seen here:
<%@categories.each do |c|%>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li class="article-title">
<strong><%= link_to "#{c.name}", category_path(c)%></strong>
<% c.sub_categories.each do |s|%>
<div>
<%=link_to s.name, category_sub_category_path(c,s)%>
</div>
<%end%>
</li>
<li><small>
<!--Pluralise here-->
</small></li>
</div>
</div>
</ul>
<%end%>
And then in my SubCategory show.html.erb
:
<span class ="badge"><%= link_to "Edit sub category name",
edit_category_sub_category_path([@category, @subcategory])%></span>
And in my SubCategory edit.html.erb
:
<%=form_for([@category, @subcategory], :html => {class: "form-horizontal", role: "form"})do |f|%>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :subcategory%>
</div>
<div class="col-sm-8">
<%= f.text_field :name, class: "form-control", placeholder: "Update Sub Category", autofocus: true%>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%=f.submit class: "btn btn-primary btn-lg"%>
</div>
</div>
<%end%>
Any help would be appreciated
EDIT:
Errors am now getting this error
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"81lwz0NufTqdOni+4wPRO4Vnmeis9f32+sqSNsxTXZY7ufyR8hTvF86KQwdMjcOP2DQJibK66croW48Uhos9+A==", "sub_category"=>{"name"=>"Gold", "category_id"=>"1"}, "commit"=>"Update Sub category", "category_id"=>"1", "id"=>"1"}
because wrong number of arguments (given 2 expects 1)
def
subcategory_params params.require(:sub_category).require(:name, :category_id)
end
FIXED: Just took out the second .require, no idea why I put that there
Upvotes: 0
Views: 42
Reputation: 1413
As you have mentioned in the comments the edit action must be like
def edit
@subcategory = SubCategory.find(params[:id])
end
So the value @category
is nil this has caused a error first value of a form cannot be nil
.
Make the edit action as
def edit
@subcategory = SubCategory.find(params[:id])
@category = @subcategory.category
end
Assuming that you have relation between category and sub category.
Upvotes: 2