I have categories nested inside of guides. I'm building an app to learn rails better and I'm trying to make a page that will display all categories that belong to a guide and have edit inputs under them and a save button next to it so the user can edit the names of the categories they want to change.
Bit stuck on how exactly how get this done.
here is the category_item_keys controller
def edit
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@key = @category.category_item_keys
end
def update
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@key = @category.category_item_keys.friendly.find(key_params) # no idea what to make this equal because it isn't one set key being edited on the page
if @key = @category.category_item_keys.update_attributes(key_params)
flash[:success] = "Key updated"
redirect_to @guide
else
render 'edit'
end
end
private
def key_params
params.require(:category_item_key).permit(:key, :slug)
end
routes
match '/guides/:guide_id/:category_id/keys/edit' => 'category_item_keys#edit', :via => :get
match '/guides/:guide_id/:category_id/keys/' => 'category_item_keys#update', :via => :post, as: :category_item_keys_update
edit.html.erb
<ul>
<% @key.each do |key| %>
<li><%= key.key #just shows key name %><br>
<%= form_for([@category, @keys], url: category_item_keys_create_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :key, "Key name" %>
<%= f.text_field :key %>
<%= f.submit "Save" %>
<% end %>
</li>
<% end %>
</ul>
This just gives me an error of:
undefined method `to_key' for #<CategoryItemKey::ActiveRecord_Associations_CollectionProxy:0x007fe20a86b480>
Later I plan on using an in-place editor gem but i would like to learn how this can be done fist.
EDIT:
Fixed the error ( changed form_for([@category, @keys]
to form_for([@category, key]
and turns out this way works for displaying and allowing all categories to be edited... to an extent.
I get another error when i submit a form
undefined method 'update_attribute'
EDIT 2
slowly getting there. I Changed the update @key variable to @key = @category.category_item_keys.all
to fix the error. But this line is now giving me problems
if @key = @category.category_item_keys.update_attributes(key_params)
'
THIRD EDIT
ERROR
Couldn't find CategoryItemKey without an ID
on line
@key = @category.category_item_keys.find params[:id]
paramaters:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"egj/OebdSbxxaoaTkr46WVIOIIu4Ezijzu45kqxLT0krjFWHqi67SRJDSgV7bcL6SeoGpUSYsrolspylCXBu9g==",
"category_item_key"=>{"name"=>"def1111"},
"commit"=>"Save",
"guide_id"=>"dbz",
"category_id"=>"characters"}
Looks like you are trying to do
update_attributes
on a collection instead of an object. Try to first fetch the key objectand then try to update its attributes
Here's how to clean up the code:
If you wanted to use an
in-place
editor gem, I'd recommend looking atX-Editable
, as we've applied it here (its only a demo app, just sign up for free and go to profile):use nested forms available in rails 4.