Change form (create to update) using javascript
I have a simple review system where users can select a number of stars and this creates an AJAX call to the create
action (if the review is new).
If the review already exists, I want it to instead call the update
action.
Right now, if the page loads to a blank review, the first change will call create
, but so will all subsequent requests until I completely reload the page.
How can I get create.js.erb
to change the reviews form from create
to update
without refreshing the page?
Clar开发者_运维技巧ification
If a particular user enters the page for the first time, they should be able to create a review, then edit their own review, without needing to refresh the page. My problem is that the create then update transition isn't happening.
I don't understand why you would want that. Is it really implemented the way that there is only one review and it gets updated over and over again by more and more users? Because an obvious implementation would be to create a completely new review every time and never use update action.
I mean:
class ItemReviewsController < ApplicationController
def create
@item = Item.find(params[:item_id])
@item.reviews.create(params[:review]) # TODO check for errors and redirect
end
end
with routes:
resources :items do
resources :reviews
end
If I am wrong, please write more about your motivation for that solution.
精彩评论