mta_json 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +130 -0
  5. data/Rakefile +1 -0
  6. data/examples/crud/README.md +40 -0
  7. data/examples/crud/crud.lua +113 -0
  8. data/examples/crud/meta.xml +14 -0
  9. data/examples/crud/mta/crud/crud.lua +0 -0
  10. data/examples/crud/mta/crud/meta.xml +3 -0
  11. data/examples/crud/rails/.gitignore +15 -0
  12. data/examples/crud/rails/Gemfile +40 -0
  13. data/examples/crud/rails/Rakefile +7 -0
  14. data/examples/crud/rails/app/assets/images/rails.png +0 -0
  15. data/examples/crud/rails/app/assets/javascripts/application.js +15 -0
  16. data/examples/crud/rails/app/assets/javascripts/posts.js.coffee +3 -0
  17. data/examples/crud/rails/app/assets/stylesheets/application.css +13 -0
  18. data/examples/crud/rails/app/assets/stylesheets/posts.css.scss +3 -0
  19. data/examples/crud/rails/app/assets/stylesheets/scaffolds.css.scss +69 -0
  20. data/examples/crud/rails/app/controllers/application_controller.rb +3 -0
  21. data/examples/crud/rails/app/controllers/posts_controller.rb +83 -0
  22. data/examples/crud/rails/app/helpers/application_helper.rb +2 -0
  23. data/examples/crud/rails/app/helpers/posts_helper.rb +2 -0
  24. data/examples/crud/rails/app/mailers/.gitkeep +0 -0
  25. data/examples/crud/rails/app/models/.gitkeep +0 -0
  26. data/examples/crud/rails/app/models/post.rb +3 -0
  27. data/examples/crud/rails/app/views/layouts/application.html.erb +14 -0
  28. data/examples/crud/rails/app/views/posts/_form.html.erb +25 -0
  29. data/examples/crud/rails/app/views/posts/edit.html.erb +6 -0
  30. data/examples/crud/rails/app/views/posts/index.html.erb +25 -0
  31. data/examples/crud/rails/app/views/posts/new.html.erb +5 -0
  32. data/examples/crud/rails/app/views/posts/show.html.erb +15 -0
  33. data/examples/crud/rails/config/application.rb +64 -0
  34. data/examples/crud/rails/config/boot.rb +6 -0
  35. data/examples/crud/rails/config/database.yml +25 -0
  36. data/examples/crud/rails/config/environment.rb +5 -0
  37. data/examples/crud/rails/config/environments/development.rb +37 -0
  38. data/examples/crud/rails/config/environments/production.rb +67 -0
  39. data/examples/crud/rails/config/environments/test.rb +37 -0
  40. data/examples/crud/rails/config/initializers/backtrace_silencers.rb +7 -0
  41. data/examples/crud/rails/config/initializers/inflections.rb +15 -0
  42. data/examples/crud/rails/config/initializers/mime_types.rb +5 -0
  43. data/examples/crud/rails/config/initializers/secret_token.rb +7 -0
  44. data/examples/crud/rails/config/initializers/session_store.rb +8 -0
  45. data/examples/crud/rails/config/initializers/wrap_parameters.rb +14 -0
  46. data/examples/crud/rails/config/locales/en.yml +5 -0
  47. data/examples/crud/rails/config/routes.rb +61 -0
  48. data/examples/crud/rails/config.ru +4 -0
  49. data/examples/crud/rails/db/migrate/20130320115007_create_posts.rb +10 -0
  50. data/examples/crud/rails/db/schema.rb +23 -0
  51. data/examples/crud/rails/db/seeds.rb +7 -0
  52. data/examples/crud/rails/lib/assets/.gitkeep +0 -0
  53. data/examples/crud/rails/lib/tasks/.gitkeep +0 -0
  54. data/examples/crud/rails/log/.gitkeep +0 -0
  55. data/examples/crud/rails/public/404.html +26 -0
  56. data/examples/crud/rails/public/422.html +26 -0
  57. data/examples/crud/rails/public/500.html +25 -0
  58. data/examples/crud/rails/public/favicon.ico +0 -0
  59. data/examples/crud/rails/public/index.html +241 -0
  60. data/examples/crud/rails/public/robots.txt +5 -0
  61. data/examples/crud/rails/script/rails +6 -0
  62. data/examples/crud/rails/test/fixtures/.gitkeep +0 -0
  63. data/examples/crud/rails/test/fixtures/posts.yml +9 -0
  64. data/examples/crud/rails/test/functional/.gitkeep +0 -0
  65. data/examples/crud/rails/test/functional/posts_controller_test.rb +49 -0
  66. data/examples/crud/rails/test/integration/.gitkeep +0 -0
  67. data/examples/crud/rails/test/performance/browsing_test.rb +12 -0
  68. data/examples/crud/rails/test/test_helper.rb +13 -0
  69. data/examples/crud/rails/test/unit/.gitkeep +0 -0
  70. data/examples/crud/rails/test/unit/helpers/posts_helper_test.rb +4 -0
  71. data/examples/crud/rails/test/unit/post_test.rb +7 -0
  72. data/examples/crud/rails/vendor/assets/javascripts/.gitkeep +0 -0
  73. data/examples/crud/rails/vendor/assets/stylesheets/.gitkeep +0 -0
  74. data/examples/crud/rails/vendor/plugins/.gitkeep +0 -0
  75. data/lib/mta_json/version.rb +3 -0
  76. data/lib/mta_json/wrapper.rb +120 -0
  77. data/lib/mta_json.rb +6 -0
  78. data/mta_json.gemspec +23 -0
  79. metadata +226 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mta_json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marcus Bauer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # mta_json
2
+
3
+ Wraps MTA:SA's JSON format to support named parameters and different HTTP
4
+ methods with callRemote.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mta_json'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mta_json
19
+
20
+ ---
21
+
22
+ You'll need to add the following line to your `config/application.rb`:
23
+
24
+ config.middleware.insert_before Rack::MethodOverride, MtaJson::Wrapper
25
+
26
+ ## Usage
27
+
28
+ ### Rails
29
+
30
+ There are no changes required to any specific code, so long as your controller
31
+ actions respond to JSON and returns JSON.
32
+
33
+ The following would just work fine:
34
+
35
+ # route:
36
+ # resources :posts, :only => [:show, :index]
37
+ class PostsController < ApplicationController
38
+ def index
39
+ @posts = Post.all
40
+
41
+ respond_to do |format|
42
+ format.html
43
+ format.json { render :json => @posts }
44
+ end
45
+ end
46
+
47
+ def show
48
+ @post = Post.find(params[:id])
49
+
50
+ respond_to do |format|
51
+ format.html
52
+ format.json { render :json => @post }
53
+ end
54
+ end
55
+ end
56
+
57
+ ### MTA:SA
58
+
59
+ The basic parameters for callRemote are:
60
+
61
+ callRemote(url, your_function, params, options)
62
+
63
+ where
64
+
65
+ * **params** will be present as `params` hash in your application.
66
+ For example,
67
+
68
+ <pre>callRemote(url, your_function, { name = 'mabako', password = '****' })</pre>
69
+
70
+ will allow you to use `params[:name]` and `params[:password]`
71
+
72
+ * **options** contained options as hints to the JSON-Wrapper.
73
+ * **method** overwrites the default POST HTTP verb, only 'GET' is a valid
74
+ alternative currently.
75
+
76
+ The format is not `posts.json` for URLs but `posts.mta`. This gem will rewrite
77
+ the URL to be `posts.json` as well as handle MTA-specific code.
78
+
79
+ The following example code shows just how you'd retrieve the list of posts
80
+ returned by above Rails-specific example.
81
+
82
+ callRemote('http://localhost/posts.mta',
83
+ function(posts, err)
84
+ if posts ~= 'ERROR' then
85
+ outputDebugString('Retrieved ' .. #posts .. ' posts: ')
86
+ for k, post in ipairs(posts) do
87
+ -- assumes your @post in rails has a 'text' attribute
88
+ outputDebugString(post.text)
89
+ end
90
+ else
91
+ outputDebugString('Failed to retrieve blog posts, error: ' .. tostring(err), 2)
92
+ end
93
+ end,
94
+ nil, # no parameters to pass
95
+ {method = 'GET'} # MTA has POST as default, pick GET instead
96
+ )
97
+
98
+ Likewise to retrieve a single post:
99
+
100
+ callRemote('http://localhost/posts/1.mta',
101
+ function(post, err)
102
+ if post ~= 'ERROR' then
103
+ -- assumes your @post in rails has a 'text' attribute
104
+ outputDebugString(post.text)
105
+ else
106
+ outputDebugString('Failed to retrieve blog post, error: ' .. tostring(err), 2)
107
+ end
108
+ end,
109
+ nil,
110
+ {method = 'GET'}
111
+ )
112
+
113
+ A more complete example can be found in `examples/crud`.
114
+
115
+ ## Contributing
116
+
117
+ 1. Fork it
118
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
119
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
120
+ 4. Push to the branch (`git push origin my-new-feature`)
121
+ 5. Create new Pull Request
122
+
123
+ ## TODOs
124
+
125
+ * Special care should be taken with the default POST method, there currently is
126
+ no way to set a CSRF-Token. This can be worked around by using:
127
+
128
+ <pre>skip_before_filter :verify_authenticity_token, :only => :your_method</pre>
129
+
130
+ * The code doesn't insert itself as middleware automatically, see Installation section.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ # CRUD Example
2
+ (Create, Read, Update, Delete)
3
+
4
+ Tests whether you can create, read, update and delete posts from MTA
5
+
6
+ ## Running the example
7
+
8
+ Start the rails server:
9
+
10
+ $ cd rails
11
+ $ rails s
12
+
13
+ Start the MTA Server and type the following commands:
14
+
15
+ $ aclrequest crud allow all
16
+ $ start crud
17
+
18
+ After that, the following commands are available within MTA (console, in-game)
19
+
20
+ * **/posts** - lists all posts
21
+
22
+ ## How this was built - in rails
23
+
24
+ Creating the project:
25
+
26
+ $ rails new CrudTest
27
+
28
+ **Manually** adding "gem 'mta_json'" to the Gemfile.
29
+
30
+ Running Bundler:
31
+
32
+ $ bundle install
33
+
34
+ Generating a model Post along with a matching Controller:
35
+
36
+ $ rails g scaffold post title:string text:text
37
+
38
+ Migrates the database to include the new posts table:
39
+
40
+ $ rake db:migrate
@@ -0,0 +1,113 @@
1
+ -- lists the -id- and -title- of all posts. While there is a text attribute, we skip that
2
+ local url = 'http://localhost:3000/'
3
+
4
+ addCommandHandler('posts',
5
+ function(player, command)
6
+ callRemote(url .. 'posts.mta',
7
+ function(posts, err)
8
+ if posts ~= 'ERROR' then
9
+ output(player, #posts .. ' posts:')
10
+ for _, post in ipairs(posts) do
11
+ output(player, ' #' .. post.id .. ': ' .. tostring(post.title))
12
+ end
13
+ else
14
+ output(player, 'Failed to fetch posts')
15
+ end
16
+ end,
17
+ nil,
18
+ { method = 'GET' }
19
+ )
20
+ end
21
+ )
22
+
23
+ addCommandHandler('post',
24
+ function(player, command, id)
25
+ if tonumber(id) then
26
+ callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
27
+ function(post, err)
28
+ if post ~= 'ERROR' then
29
+ output(player, 'Post #' .. post.id .. ': ' .. tostring(post.title))
30
+ -- first 60 characters only
31
+ output(player, tostring(post.text):sub(1, 60))
32
+ else
33
+ output(player, 'Failed to fetch post')
34
+ end
35
+ end,
36
+ nil,
37
+ { method = 'GET'}
38
+ )
39
+ else
40
+ output(player, 'Missing id, /' .. command .. ' id')
41
+ end
42
+ end
43
+ )
44
+
45
+ addCommandHandler('newpost',
46
+ function(player, command, title, ...)
47
+ callRemote(url .. 'posts.mta',
48
+ function(post, err)
49
+ if post ~= 'ERROR' then
50
+ output(player, 'Created Post #' .. post.id .. ': ' .. tostring(post.title))
51
+ -- first 60 characters only
52
+ output(player, tostring(post.text):sub(1, 60))
53
+
54
+ -- rails sets the location like this:
55
+ -- format.json { render json: @post, status: :created, location: @post }
56
+ -- err contains our headers if no error was received.
57
+ output(player, 'URL: ' .. err['Location'])
58
+ else
59
+ output(player, 'Failed to create post')
60
+ end
61
+ end,
62
+ { post = { title = title, text = table.concat({...}, ' ') } }
63
+ )
64
+ end
65
+ )
66
+
67
+ addCommandHandler('updatepost',
68
+ function(player, command, id, title, ...)
69
+ if tonumber(id) then
70
+ callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
71
+ function(post, err)
72
+ if post ~= 'ERROR' then
73
+ -- the controller generated by the default update has
74
+ -- head :no_content - which means both post and err will be empty.
75
+ output(player, 'Updated!')
76
+ else
77
+ output(player, 'Failed to update post')
78
+ end
79
+ end,
80
+ { post = { title = title, text = table.concat({...}, ' ') } },
81
+ { method = 'PUT' }
82
+ )
83
+ end
84
+ end
85
+ )
86
+
87
+ addCommandHandler('delpost',
88
+ function(player, command, id)
89
+ if tonumber(id) then
90
+ callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
91
+ function(post, err)
92
+ if post ~= 'ERROR' then
93
+ -- the controller generated by the default update has
94
+ -- head :no_content - which means both post and err will be empty.
95
+ output(player, 'deleted!')
96
+ else
97
+ output(player, 'Failed to delete post')
98
+ end
99
+ end,
100
+ nil,
101
+ { method = 'DELETE' }
102
+ )
103
+ end
104
+ end
105
+ )
106
+
107
+ function output(element, text)
108
+ if getElementType(element) == 'console' then
109
+ outputServerLog(text)
110
+ else
111
+ outputChatBox(text, element)
112
+ end
113
+ end
@@ -0,0 +1,14 @@
1
+ <!--
2
+ mta_json crud example to interact with rails.
3
+
4
+ See https://github.com/mabako/mta_json
5
+ -->
6
+ <meta>
7
+ <!-- Script source file -->
8
+ <script src="crud.lua"/>
9
+
10
+ <!-- This wouldn't make sense without callRemote, would it? -->
11
+ <aclrequest>
12
+ <right name="function.callRemote" access="true" />
13
+ </aclrequest>
14
+ </meta>
File without changes
@@ -0,0 +1,3 @@
1
+ <meta>
2
+ <script src="crud.lua"/>
3
+ </meta>
@@ -0,0 +1,15 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp
@@ -0,0 +1,40 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '3.2.12'
4
+
5
+ # Bundle edge Rails instead:
6
+ # gem 'rails', :git => 'git://github.com/rails/rails.git'
7
+
8
+ gem 'sqlite3'
9
+
10
+
11
+ # Gems used only for assets and not required
12
+ # in production environments by default.
13
+ group :assets do
14
+ gem 'sass-rails', '~> 3.2.3'
15
+ gem 'coffee-rails', '~> 3.2.1'
16
+
17
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
18
+ # gem 'therubyracer', :platforms => :ruby
19
+
20
+ gem 'uglifier', '>= 1.0.3'
21
+ end
22
+
23
+ gem 'jquery-rails'
24
+
25
+ # To use ActiveModel has_secure_password
26
+ # gem 'bcrypt-ruby', '~> 3.0.0'
27
+
28
+ # To use Jbuilder templates for JSON
29
+ # gem 'jbuilder'
30
+
31
+ # Use unicorn as the app server
32
+ # gem 'unicorn'
33
+
34
+ # Deploy with Capistrano
35
+ # gem 'capistrano'
36
+
37
+ # To use debugger
38
+ # gem 'debugger'
39
+
40
+ gem 'mta_json'
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ CrudTest::Application.load_tasks
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,3 @@
1
+ # Place all the behaviors and hooks related to the matching controller here.
2
+ # All this logic will automatically be available in application.js.
3
+ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,3 @@
1
+ // Place all the styles related to the posts controller here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,69 @@
1
+ body {
2
+ background-color: #fff;
3
+ color: #333;
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ p, ol, ul, td {
10
+ font-family: verdana, arial, helvetica, sans-serif;
11
+ font-size: 13px;
12
+ line-height: 18px;
13
+ }
14
+
15
+ pre {
16
+ background-color: #eee;
17
+ padding: 10px;
18
+ font-size: 11px;
19
+ }
20
+
21
+ a {
22
+ color: #000;
23
+ &:visited {
24
+ color: #666;
25
+ }
26
+ &:hover {
27
+ color: #fff;
28
+ background-color: #000;
29
+ }
30
+ }
31
+
32
+ div {
33
+ &.field, &.actions {
34
+ margin-bottom: 10px;
35
+ }
36
+ }
37
+
38
+ #notice {
39
+ color: green;
40
+ }
41
+
42
+ .field_with_errors {
43
+ padding: 2px;
44
+ background-color: red;
45
+ display: table;
46
+ }
47
+
48
+ #error_explanation {
49
+ width: 450px;
50
+ border: 2px solid red;
51
+ padding: 7px;
52
+ padding-bottom: 0;
53
+ margin-bottom: 20px;
54
+ background-color: #f0f0f0;
55
+ h2 {
56
+ text-align: left;
57
+ font-weight: bold;
58
+ padding: 5px 5px 5px 15px;
59
+ font-size: 12px;
60
+ margin: -7px;
61
+ margin-bottom: 0px;
62
+ background-color: #c00;
63
+ color: #fff;
64
+ }
65
+ ul li {
66
+ font-size: 12px;
67
+ list-style: square;
68
+ }
69
+ }
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,83 @@
1
+ class PostsController < ApplicationController
2
+ # GET /posts
3
+ # GET /posts.json
4
+ def index
5
+ @posts = Post.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.json { render json: @posts }
10
+ end
11
+ end
12
+
13
+ # GET /posts/1
14
+ # GET /posts/1.json
15
+ def show
16
+ @post = Post.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.json { render json: @post }
21
+ end
22
+ end
23
+
24
+ # GET /posts/new
25
+ # GET /posts/new.json
26
+ def new
27
+ @post = Post.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.json { render json: @post }
32
+ end
33
+ end
34
+
35
+ # GET /posts/1/edit
36
+ def edit
37
+ @post = Post.find(params[:id])
38
+ end
39
+
40
+ # POST /posts
41
+ # POST /posts.json
42
+ def create
43
+ @post = Post.new(params[:post])
44
+
45
+ respond_to do |format|
46
+ if @post.save
47
+ format.html { redirect_to @post, notice: 'Post was successfully created.' }
48
+ format.json { render json: @post, status: :created, location: @post }
49
+ else
50
+ format.html { render action: "new" }
51
+ format.json { render json: @post.errors, status: :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /posts/1
57
+ # PUT /posts/1.json
58
+ def update
59
+ @post = Post.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @post.update_attributes(params[:post])
63
+ format.html { redirect_to @post, notice: 'Post was successfully updated.' }
64
+ format.json { head :no_content }
65
+ else
66
+ format.html { render action: "edit" }
67
+ format.json { render json: @post.errors, status: :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /posts/1
73
+ # DELETE /posts/1.json
74
+ def destroy
75
+ @post = Post.find(params[:id])
76
+ @post.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to posts_url }
80
+ format.json { head :no_content }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module PostsHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+ attr_accessible :text, :title
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>CrudTest</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,25 @@
1
+ <%= form_for(@post) do |f| %>
2
+ <% if @post.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @post.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :title %><br />
16
+ <%= f.text_field :title %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :text %><br />
20
+ <%= f.text_area :text %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit %>
24
+ </div>
25
+ <% end %>