Multiple Coffee Template

I have a task model that is associated with the user and project models.

When I create / update a task, I need to do an update in the async view not only to change / add the task, but also for project and user information (because some of this data may also change).

I have this in the controller:

 def create @task = Task.new(params[:task]) @project = Project.find(params[:project_id]) respond_to do |format| if @task.save format.html { redirect_to @task, notice: 'Task was successfully created.' } format.json { render json: @task, status: :created, location: @task } else format.html { render action: "new" } format.json { render json: @task.errors, status: :unprocessable_entity } end end end 

And my tasks /create.js.coffee

 # Update task table $('#mytable').append("<%= j render(partial: 'tasks/task', locals: { t: @task }) %>") # Update user data $('.user-data').html("<%= j render(partial: 'users/user_widget', locals: { u: current_user }) %>") # Update project data $('.project-data').html("<%= j render(partial: 'projects/project_widget', locals: { p: @project }) %>") 

And it works great. I see 2 questions:

  • In each .js.coffee .js.coffee add, I repeat the code too much. I duplicate exactly the same code for updating project and user data, updating tasks, destroying tasks, and I would do the same for the new model, which could affect the user and the project

  • It seems strange to process project and user data in tasks /create.js.coffee

Therefore, I am looking for the best sample for processing this material, any ideas?

EDIT (clarify): I think that achieving something like this would be better:

Tasks / create.js.coffee

 # Update task table $('#mytable').append("<%= j render(partial: 'tasks/task', locals: { t: @task }) %>") UserData.refresh() ProjectData.refresh() 

However, I cannot do this because I need to expose a particle every time, so I would have to do something strange, like passing partial html parts to those refresh() functions, and that would be very similar to the previous method. This is just a way that came to my mind, but I would like to hear your ideas, if any.

+6
source share
2 answers

For those who may be interested in answering this question, I found the Garber-Irish pattern as a great solution to my problem.

You can read more about this here: https://www.viget.com/articles/extending-paul-irishs-comprehensive-dom-ready-execution

There is even a gem: https://github.com/tonytonyjan/gistyle

Using this template, you can have runtime contexts for your HTTP requests. For example, you can have an afterCreate() method for each js controller, and depending on where your HTTP request was created, you can trigger another event.

This allows you to use polymorphic responses to update the view.

0
source

You can display a template / action belonging to another controller. Thus, you can save the tasks / create.js.coffee file for all other controller actions (such as users and projects) that use the same code in your response_to block that you would use:

 format.json { render 'tasks/create' } 

You can even display a specific file:

 format.json { render file: "path/to/specific/file" } 

Here is a link to more information about rendering in rails: http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-an-action-s-template-from-another-controller

+1
source

Source: https://habr.com/ru/post/1013342/


All Articles