Answer the question
In order to leave comments, you need to log in
How to make user reviews from other users?
I can't set up user reviews from users for the third day. The
following is done:
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.text :content
t.timestamps
end
end
end
users_reviews
class CreateUsersReviews < ActiveRecord::Migration
def change
create_table :users_reviews do |t|
t.references :for_user, index: true
t.references :by_user, index: true
t.timestamps
end
end
end
forgot to add review_id to Users_review
class AddReviewIdToUsersReviews < ActiveRecord::Migration
def change
add_reference :users_reviews, :review, index: true
end
end
then i create associations
class User < ActiveRecord::Base
has_many :users_review
has_many :reviews, :through => :users_review
class Review < ActiveRecord: :Base
has_many :users_reviews
has_many :users, :through => :users_reviews
end
class UsersReview < ActiveRecord::Base
belongs_to :user
belongs_to :review
end
then controller
class ReviewsController < ApplicationController
def new
@user = User.find(params[:user_id] )
@review = @user.reviews.new(params[:user_id])
end
def create
@user = User.find(params[:user_id])
@review = @user.reviews.build(review_params)
@review.save
redirect_to root_path
end
def show
end
def index
@user = User.find(params[:for_user_id])
@reviews = Review.all
end
private
def review_params
params.require(:review).permit(:for_user_id, :by_user_id, :content)
end
end
and my view
<%= form_for([@user, @user.reviews.build]) do |f| %>
<%= f.text_area :content, placeholder: "Your review" %>
<%= f.submit "GO", class: "btn btn-large btn-primary" %>
<% end %>
rails save data to table reviews but do not want to save the data in the join table, what should I do?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question