我正在为学校建立一个审查申请。用户只有在登录后才能查看学校。
你现在可以做什么:
您可以撰写评论并添加 1 - 5 之间的评分。应用程序会保存评论。
我想要它做什么:
我想保存记录的 user_id。因此,评论属于用户和学校。
但我对如何做到这一点有点困惑。欢迎任何想法和指导方针=)
审查模型:
class Review < ActiveRecord::Base
belongs_to :school
belongs_to :user
attr_accessible :content, :rating
end
学校型号:
class School < ActiveRecord::Base
has_many :reviews
..more code..
end
用户型号:
class User < ActiveRecord::Base
has_many :reviews
...more code..
end
审核控制人:
class ReviewsController < ApplicationController
def create
@school = School.find(params[:school_id])
@review = @school.reviews.create!(params[:review])
redirect_to @review.school, notice: "Review has been created."
end
end
审查表
<%= form_for [@school, Review.new] do |f| %>
<h3>Write a review</h3>
<div class="reviews_comments_container">
<div class="review_box">
<ol class="radio-rater">
<%= f.radio_button(:rating, 1) %>
<%= f.radio_button(:rating, 2) %>
<%= f.radio_button(:rating, 3) %>
<%= f.radio_button(:rating, 4) %>
<%= f.radio_button(:rating, 5) %>
</ol>
</div>
<div class="review_box" id="review_textarea">
<%= f.text_area :content, rows: 4, cols: 70 %>
</div>
<div class="review_box">
<%= f.submit 'Save your review', :class => "btn" %>
</div>
</div>
<% end %>
迁移文件:
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.text :content
t.integer :rating
t.belongs_to :school
t.belongs_to :user
t.timestamps
end
add_index :reviews, [:school_id, :user_id]
end
end