D
D
dexdev2014-10-13 19:31:18
Ruby on Rails
dexdev, 2014-10-13 19:31:18

How to properly nested form on another view and use another controller?

Hello everyone, I just can't figure out how to organize a nested form!
We have:
Page /posts/:id/ show action, on the page a form for adding a review

<%= form_for([@post, @post.reviews.build]) do |review_form| %>
      <div class="create_review_rb">
      <%= review_form.text_area :body, class: "form-control" %>
      <%= review_form.label :negative, "Положительный отзыв" %>
      <%= review_form.radio_button :negative, 1, checked: true %>
      <%= review_form.label :negative, "Отрицательный отзыв" %>
      <%= review_form.radio_button :negative, 0 %>
      <%= review_form.hidden_field :user_id, value: current_user.id %>
      <%= review_form.hidden_field :for_user_id, value: @post.executor.id %>
      </div>
      <%= review_form.fields_for :post do |f| %>
       <%= f.text_field :final, class: "form-control" %>
       <%= f.label :status, "статус" %>
    <%=f.radio_button :status, 3, checked: true %>
    <%= f.label :status, "статус" %>
    <%=f.radio_button :status, 4 %>
       <% end %>
      <%= review_form.submit "Добавить отзыв", class: "btn btn-large btn-primary", id: 'review_button' %>
    <% end %>

Which should pass through ReviewsController
class ReviewsController < ApplicationController

  def new
    @post = Post.find(params[:post_id])
    	@review = @post.reviews.new
    end

  def create
    @post = Post.find(params[:post_id])
    	@review = @post.reviews.build(review_params)
    	if @review.save
    		flash[:success] = "Поздравляем Ваш отзыв добавлен"
    		redirect_to completed_post_path(@post)
    	else
    		render 'new'
    	end
  end

  private

  def review_params
     	params.require(:review).permit(:negative, :post_id, :body, :user_id, :for_user_id, post_attributes:[:finalcustomerprice, :status, :id]).merge(:user_id => current_user.id)
  end
end

indicated in the model
class Review < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
    accepts_nested_attributes_for :post
end

for all this, rails gives the error Couldn't find Post with ID=41 for Review with ID=
and if you remove accepts_nested_attributes_for : post it is not clear how to bypass strong parameters

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2014-10-13
@viktorvsk

It's unclear. Maybe the post still has attributes?
And why should the post form submit for review? Checked in general, whether there goes the request? And apparently your review is being sent without an ID...
And if there is no post with ID 41, then it won’t be.
Now it feels like you have a review site where you can link a post to each review

V
vsuhachev, 2014-10-14
@vsuhachev

1) you created @review in both methods 2) As far as I understand, you want to rate the post with each review, averaging it. The path you have chosen is wrong. Typically, the score is stored in each review and then the average is calculated using SQL functions. Accordingly, accepts_nested_attributes_for and fields_for are not needed in review. In general, it would be more useful not to throw code here and ask what doesn’t work, but describe the final task that you are solving and ask how best to implement it. There will be more replies :) form_for([@post, @review])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question