2

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.

3
  • If you want to create a object (= persist it in the DB), you have to use the .create method: Word.create(filter_params) ; when you redirect to user_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) Commented May 30, 2014 at 17:57
  • @MrYoshiji Yes, but still, how do I make a proper AJAX request? Commented May 30, 2014 at 18:12
  • @danya why can't you simply make a link with its path to users create action? Commented May 30, 2014 at 20:26

1 Answer 1

2

In rails you can put remote: true on any link to make it ajax-y. In your case it sounds like the problem is you don't have a legitimate link to a create action. So what you need is:

First: A create action in the Words controller:

def create
   @word = Word.new(word_params)
   @word.user = current_user
   @word.save!
end

...

private
def word_params
   params.require(:word).permit(:word, :translation, :context)
end

Second: A link to the create action on your page:

<%= link_to(
   "Test",
   words_path(word: "Test", translation: "foo", context: "bar"),
   method: :post,
   remote: true 
) %>

The important points are:

  1. You link to the words_path, which is the helper method for the words resource.
  2. You set the method to post to inform your controller that you want the create action.

You can find the mapping of helpers and methods to controller actions by running rake routes.

A few caveats:

  1. I don't really know your model well enough but it looks from your question like Word has columns word, translation, and context, and an association to a user. Adjust the above code as needed.
  2. You might want to make your create action smarter than I have shown above. My version will simply raise an exception if you fail validation. I leave it to you to make it do what you need.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.