我已实施以下
user.rb
has_many :authored_feedbacks, :class_name => "Feedback", :foreign_key => "author_id", :dependent => :destroy
has_many :received_feedbacks, -> { order("id DESC")}, :class_name => "Feedback", :foreign_key => "recipient_id", :dependent => :destroy
feedback.rb
belongs_to :author, :foreign_key => :author_id, :class_name => "User"
belongs_to :recipient, :foreign_key => :recipient_id, :class_name => "User"
belongs_to :listing
feedbacks_controller.rb
def new
@author = current_user
@listing = Listing.find(params[:listing_id])
@recipient = @listing.user
@feedback = Feedback.new
@feedback_presenter = FeedbackPresenter.new(@listing)
end
def create
@author = current_user
@listing = Listing.find(params[:listing_id])
@recipient = @listing.user
@feedback = @author.authored_feedbacks.build(listing_id: @listing.id, author_id: @author.id, recipient_id: @recipient.id, rating: params[:feedback][:rating], comment: params[:feedback][:comment])
if @feedback.save
flash[:notices] = ["Feedback successfully submitted"]
redirect_to user_feedbacks_path(@user)
else
render 'new'
end
end
private
def feedback_params
params.require(:feedback).permit! (just ensure it's not a whitelist issue)
end
from listings / show.html.erb我链接创建一个新的反馈:
<%= link_to 'Leave feedback for this user', new_user_feedback_path(@user, @feedback, listing_id: @listing.id, author_id: current_user.id, recipient_id: @listing.user.id) %>
反馈/ new.html.erb
<%= form_for [@author, @feedback], url: user_feedbacks_path(@author, @feedback) do |f| %>
<%= f.label :rating %>
<%= f.select(:rating, @feedback_presenter.feedback_options, {}, id: 'rating', class: 'form-control') %>
<%= f.label :comment %>
<%= f.text_field :comment, class: "form-control" %>
<%= f.submit "Submit Feedback", class: "btn btn-primary" %>
<% end %>
我对协会设置感到满意,并且相信我正在通过所有必需的参数,但是我正在创建任何反馈记录。我不确定我在哪里出错了所以想在这里询问SO。
编辑 - 添加了服务器输出:
Started POST "/users/5/feedbacks" for ::1 at 2016-03-10 22:53:02 +0800
Processing by FeedbacksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"48PdNPwx3Dbusm+4H7n87+PvUFRCJUTxLAfiaMwxvAxFQww6YVUn3j5fChwGhwqHSc0ARuo+i5h+p8QvC8vw+A==", "feedback"=>{"rating"=>"Positive", "comment"=>"312435"}, "commit"=>"Submit Feedback", "user_id"=>"5"}
Listing Load (0.2ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = $1 LIMIT 1 [["id", nil]]
Redirected to http://localhost:3000/users/1/feedbacks/new?listing_id=5&recipient_id=1
Completed 302 Found in 3ms (ActiveRecord: 0.2ms)