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.