From 9b4d055b649ad24ad1482f72d138aa73da1e4bd7 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 9 Aug 2009 11:57:44 +0200 Subject: refactored the way new menu items are created. went back to rest and added the js ajax search to fill out the form for you. much cleaner, unobtrusive approach. --- app/controllers/menu_items_controller.rb | 15 ++++------- app/views/menu_items/index.html.erb | 18 +++++-------- app/views/menu_items/new.html.erb | 34 +++++++++++++++++++++-- public/javascripts/admin_search.js | 46 +++++++++++--------------------- 4 files changed, 59 insertions(+), 54 deletions(-) diff --git a/app/controllers/menu_items_controller.rb b/app/controllers/menu_items_controller.rb index d6f0252..0ba56c3 100644 --- a/app/controllers/menu_items_controller.rb +++ b/app/controllers/menu_items_controller.rb @@ -10,20 +10,15 @@ class MenuItemsController < ApplicationController end def new + @menu_item = MenuItem.new params[:menu_item] end def create - respond_to do |format| - format.html {} - - format.js do - MenuItem.create params[:menu_item] - end - - + if MenuItem.create( params[:menu_item] ) + redirect_to menu_items_path + else + render :new end - - end def edit diff --git a/app/views/menu_items/index.html.erb b/app/views/menu_items/index.html.erb index d2ff10e..db4a771 100644 --- a/app/views/menu_items/index.html.erb +++ b/app/views/menu_items/index.html.erb @@ -1,18 +1,12 @@ +<% content_for :subnavigation do %> + <%= link_to "new", new_menu_item_path %> +<% end %> + +

Menu Items

- - \ No newline at end of file + \ No newline at end of file diff --git a/app/views/menu_items/new.html.erb b/app/views/menu_items/new.html.erb index e3f800e..64b9abb 100644 --- a/app/views/menu_items/new.html.erb +++ b/app/views/menu_items/new.html.erb @@ -1,2 +1,32 @@ -

MenuItems#new

-

Find me in app/views/menu_items/new.html.erb

+

Add Menu Item

+ +<% form_for @menu_item do |f| %> + + + + + + + + + + + + + + + + + + + + + + +
Search + <%= text_field_tag :menu_search_term %> +
+ +
+
Node Id<%= f.text_field :node_id %>
Path<%= f.text_field :path %>
Title<%= f.text_field :title %>
<%= f.submit 'Create' %>
+<% end %> \ No newline at end of file diff --git a/public/javascripts/admin_search.js b/public/javascripts/admin_search.js index 1e6f545..186e4bc 100644 --- a/public/javascripts/admin_search.js +++ b/public/javascripts/admin_search.js @@ -50,7 +50,7 @@ admin_search = { menu_items = { initialize_search : function() { - $("#search_term").bind("keyup", function() { + $("#menu_search_term").bind("keyup", function() { if ($(this).attr("value")) { $.ajax({ type: "GET", @@ -72,40 +72,26 @@ menu_items = { show_results : function(results) { $("#search_results").empty(); for (result in results) { - var foo = $((""+ results[result].title + "")); - $(foo).bind("click", function(){ - menu_items.open_title_popup(); - //menu_items.add_item_to_menu(results[result]); + var link = $((""+ results[result].title + "")); + $(link).bind("click", function(){ + menu_items.add_item_to_form(results[result]); return false; }); - $("#search_results").append(foo); + + // Sometimes I don't get jquery; wrap() didn't work *sigh* + // Guess I'll need a book someday or another framework + var wrapper = $("
"); + $(wrapper).append(link) + + $("#search_results").append(bar); } }, - open_title_popup : function() { - popup = $("
"); - $("form", popup).submit(function(){ - alert("hi"); - return false; - }); - $("body").append(popup); - }, - - add_item_to_menu : function(node) { - $.ajax({ - type: "post", - url: "/menu_items/create", - data: { - "menu_item[node_id]" : node.node_id, - "menu_item[path]" : "/" + node.unique_name, - "menu_item[title]" : node.title - }, - success : function() { - alert("s"); - } - }); - } - + add_item_to_form : function(node) { + $("#new_menu_item #menu_item_node_id").val(node.node_id); + $("#new_menu_item #menu_item_path").val("/" + node.unique_name); + $("#new_menu_item #menu_item_title").val(node.title); + } }; \ No newline at end of file -- cgit v1.3