I'm unable to get a AJAX form to work on Heroku even though it works locally. you can check out the heroku app here -
(The form is the new subreddit form located on the right of the screen)
The form code in the view (its a partial) -
<form class="form-inline" action="/subreddits" accept-charset="UTF-8" data-remote="true" method="post">
<div class="form-group">
<input class="input-small" name="utf8" type="hidden" value="✓">
</div>
<div class="form-group sr-title">
<input class="form-control input-small subreddit_title_field" placeholder="Create new subreddit" type="text" name="subreddit[title]" id="subreddit_title">
</div> <button type="submit" name="commit" class="btn btn-default new-subreddit">Submit</button>
the code in the respective controller -
def create
p params[:subreddit_title]
@subreddit = Subreddit.new(title: params[:subreddit_title].downcase)
respond_to do |format|
if @subreddit.save
format.js { render 'create.js.erb' }
else
@error = @subreddit.errors.full_messages.to_sentence
format.js { render 'error.js.erb' }
end
end
end
the javascript application code snippet (in application.js)-
$('.new-subreddit').on("click", function(e) {
e.preventDefault();
var form_data = $('.subreddit_title_field').val();
var values = {
subreddit_title: form_data,
}
var ajaxRequest = $.ajax({
url: '/subreddits',
type: 'POST', // GET, PUT, DELETE
data: values,
});
});
the output locally for "bridges" as a form input (which works)-
tarted POST "/subreddits" for ::1 at 2015-05-09 11:55:52 +0530 ActiveRecord::SchemaMigration Load (6.3ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by SubredditsController#create as / Parameters: {"subreddit_title"=>"bridges"} "bridges" (0.1ms) BEGIN Subreddit Exists (0.9ms) SELECT 1 AS one FROM "subreddits" WHERE "subreddits"."title" = 'bridges' LIMIT 1 SQL (0.4ms) INSERT INTO "subreddits" ("title", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["title", "bridges"], ["created_at", "2015-05-09 06:25:53.054220"], ["updated_at", "2015-05-09 06:25:53.054220"]] (12.3ms) COMMIT Rendered subreddits/create.js.erb (2.4ms) Completed 200 OK in 93ms (Views: 20.4ms | ActiveRecord: 23.4ms)
the output from heroku logs -
2015-05-09T06:02:56.196766+00:00 app[web.1]: Started POST "/subreddits" for 180.151.86.227 at 2015-05-09 06:02:56 +0000 2015-05-09T06:02:56.200751+00:00 app[web.1]: Processing by SubredditsController#create as / 2015-05-09T06:02:56.203206+00:00 app[web.1]: 2015-05-09T06:02:56.203210+00:00 app[web.1]: NoMethodError (undefined method downcase' for nil:NilClass): 2015-05-09T06:02:56.203212+00:00 app[web.1]: app/controllers/subreddits_controller.rb:8:increate'
Aucun commentaire:
Enregistrer un commentaire