mysql - Using AJAX POST from javascript to Rails 4 controller with Strong parameter -
i'm newbie rails
my purpose insert song_id , title received javascript via ajax post database (mysql)
in javascript file
var song_id = "23f4"; var title = "test"; $( document ).ready( function() { jquery.ajax({ url: 'create', data: "song_id=" + song_id + "&title=" + title, type: "post", success: function(data) { alert("successful"); }, failure: function() { alert("unsuccessful"); } }); } );
in editor_controller.rb
class editorcontroller < applicationcontroller def new @song = song.new end def create logger.debug("#{params[:song_id]}") logger.debug("#{params[:title]}") @song = song.new(song_params) if @song.save redirect_to root_path else flash[:notice_song_failed] = true redirect_to root_path end end private def song_params params.require(:song).permit(:song_id, :title) end
problem when running rails app code, console notices me
actioncontroller::parametermissing @ /editor/create
param missing or value empty: song
i'm trying use
private def song_params params.require(:song).permit(params[:song_id], params[:title]) end but doesn't work , notices me same, in terminal log told me below
started post "/editor/create" ::1 @ 2015-04-01 01:07:23 +0700
processing editorcontroller#create /
parameters: {"song_id"=>"23f4", "title"=>"test"}
23f4
test
completed 400 bad request in 1ms
missed in code, advance.
you not sending song parameter @ all. looks need update data line in jquery.ajax call include song parameter so:
data: {song: {song_id: song_id, title: title}}
Comments
Post a Comment