In Rails app there is a User model and User has many Words. There is a table with many items (show action in items controller), each item is represented by row which has a word and link (that will be used to create a record). How to create a word record associated with user based on row data using AJAX? For example, the row has word 'test', and when the link is clicked new Word record should be created and appended to the current user's words.
I tried to give each link remote: true attribute and make AJAX request in show.js.erb (because links are in the show action) which is called each time the link is clicked, but there is no way to find out which link was clicked, so I cannot extract any info from the row.
The AJAX request looks like this:
$.ajax({
type: "POST",
url: "/users/<%= current_user.id %>/words/",
data: { word: { word: word, translation: translation, context: context } },
});
Create action in WordsController:
def create
@words << Word.new(filter_params)
redirect_to user_words_path
end
Any help would be great.
.createmethod:Word.create(filter_params); when you redirect touser_word_path, you have to give this helper the user's id to redirect to, in your case:redirect_to user_words_path(current_user.id)