1

In my Rails 4 app, I have created a app/extra folder, where I store some custom class methods.

For instance, inside the above folder, I have a process folder, and inside this folder, I have a process_base.rb file and some other process_method1.rb, process_method2.rb, etc.

I then declare these methods in my Post model, as follows:

def process_post
  @process_method1 = Process::ProcessMethod1.new
  @process_method1.process self
  @process_method2 = Process::ProcessMethod2.new
  @process_method2.process self
end

And finally, in my Posts#show view, I use:

<%= @post.process_post %>

This is working pretty well, since I get what I need when the post page is loaded.

What I would like to do now, is to leverage these methods in the Posts#new view, and apply them in real-time to the @post being created, with JavaScript.

For instance, let's say process_method1 counts the number of characters in the @post.message, I would like to be able to count the characters as the user is typing his message, pretty much like what you can see on Twitter when you compose a new tweet.

I am not a very experienced Rails developer, and I understand this question may be broad: I am aware I can't expect a full answer with detailed code, but I would appreciate any pointer that would allow me to understand in which direction I should investigate further.

Maybe there is a name for what I am trying to achieve, and knowing what this name is would be more than enough to look for relevant resources online.

Or, maybe, it would make more sense to re-write all the logic directly in JavaScript, instead of trying to apply Ruby methods with JavaScript.

—————

UPDATE: the question here is close to what I want to achieve, except I would like to know if I can/should reuse the Ruby methods defined in my app/extra folder to avoid code and logic duplication.

—————

Any kind of help would be highly appreciated.

1 Answer 1

1

What I would like to do now, is to leverage these methods in the Posts#new view, and apply them in real-time to the @post being created, with JavaScript.

You can either create a new controller action that responds to JSON, or you can teach your existing controller action to respond to both JSON and HTML using respond_to.

See also: Action Controller Overview and Working with JavaScript in Rails

For instance .. to count the characters as the user is typing his message, pretty much like what you can see on Twitter when you compose a new tweet.

In that example, I'd recommend a new controller action, because simply counting characters is quite different from creating a new tweet. You might even want a separate controller entirely.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I agree that counting characters is different from creating a new post. What I don't really understand at that point is how I can call actions from two different controllers in the same view. If that question makes sense...
Oh, that's where javascript comes in. To continue our example, you'd use javascript to send an HTTP request to the "character count" action, e.g. for each keystroke. Asynchronous HTTP requests sent by javascript are called XHRs (the X actually stands for XML, for historical reasons, but you don't have to use XML).

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.