问题
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"}
回答1:
Here's how to clean up the code:
#config/routes.rb
resources :guides do
resources :categories, path: "" do
resources :category_item_keys, path: "keys", only: [:update] do
get :edit, on: :collection #-> url.com/guides/:guide_id/:category_id/keys/edit
end
end
end
#app/controllers/keys_controller.rb
class KeysController < ApplicationController
def edit
@guide = Guide.find params[:guide_id]
@category = Category.find params[:category_id]
@keys = @category.category_item_keys
end
def update
@guide = Guide.find params[:guide_id]
@category = Category.find params[:category_id]
@key = @category.category_item_keys.find params[:id]
if @key.update key_params
redirect_to @guide, success: "Key Updated"
else
render 'edit'
end
end
private
def key_params
params.require(:category_item_key).permit(:key)
end
end
#app/views/keys/edit.html.erb
<% @keys.each do |key| %>
<%= form_for [@guide, @category, key] do |f| %>
<%= f.text_field :key %>
<%= f.submit %>
<% end %>
<% end %>
If you wanted to use an in-place editor gem, I'd recommend looking at X-Editable, as we've applied it here (its only a demo app, just sign up for free and go to profile):
回答2:
Looks like you are trying to do update_attributes on a collection instead of an object. Try to first fetch the key object
@key = @category.category_item_keys.friendly.find(params[:id])
and then try to update its attributes
if @key.update_attributes(key_params)
...
end
回答3:
use nested forms available in rails 4.
来源:https://stackoverflow.com/questions/34736173/edit-page-to-display-all-category-names-with-edit-inputs-on-each