Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I save the currentRating in rails #56

Open
kerbong opened this issue Jan 20, 2021 · 0 comments
Open

How can I save the currentRating in rails #56

kerbong opened this issue Jan 20, 2021 · 0 comments

Comments

@kerbong
Copy link

kerbong commented Jan 20, 2021

Hi, Iam so exhausted about it... I tried so much. Pls help me.

its my _form.html.erb
image

I wanna make this system.
If user select "star-rating", it is automatley filled by rating score (like 3.5)

then user write title and desc. If user click "submit" button, then review(has title/desc/star-rating) should be created.
But, I don't know how can I grab currentRating and put it in :rating params...

(when I've been tested it, I can save ca_review but there is no :rating value... it was always nil...)

I'm using rails6.

bottom of _form.html.erb

<script> $(function() { $(".my-rating").starRating({ initialRating: 3.5, strokeColor: '#894A00', strokeWidth: 10, starSize: 35, starShape: 'rounded', onHover: function(currentIndex, currentRating, $el){ $('.live-rating').text(currentIndex); }, onLeave: function(currentIndex, currentRating, $el){ $('.live-rating').text(currentRating); const c_rating = currentRating; } }); }); </script>

middle of _form.html.erb

<%= form_with(url: care_article_ca_reviews_path, class: "shadow p-3 mb-3 bg rounded", local: true) do |f| %>


<%= obj %>

	<div class="form-group row" style= "font-size:1.5em; padding-left:1em; margin-bottom:-1em;">
      <%= f.label :rating, "평점", class: "col-12 col-form-label text-light" %>
		<div class="text-light" style= "font-size:0.5em; padding-left: 15px;"> 
			(0.5점 단위로 가능하고 한 번 누르면 고정됩니다)
		</div>
      <div class="col-11">
		<!-- 별 평점 매기기 -->
	  	<div class="my-rating jq-star" style="text-align:center;" name="rating" id="rating">
	    </div>
		<div style="text-align:center;">
			 <label class= "live-rating padding-left:4em;" ></label>
			 <%= number_field_tag 'rating', "", class: "live-rating" %>
		</div>
      </div>
```

ca_reviews_controller.rb

  def create 
    @ca_review = CaReview.new(title: params[:title], review: params[:review], care_article_id: params[:care_article_id], user_id: current_user.id, rating: params[:rating])

    respond_to do |format|
      if @ca_review.save
        format.html { redirect_to care_article_path(@care_article), notice: '돌봄 후기가 성공적으로 저장되었습니다.' }
        format.json { render :show, status: :created, location: @ca_review }
      else
        format.html { render :new }
        format.json { render json: @ca_review.errors, status: :unprocessable_entity }
      end
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant