如何创建用于审阅用户的表单

问题描述:

我想根据Hartl的教程实现用户审阅。如何创建用于审阅用户的表单

我没有问题在Rails的控制台中进行,但我遇到了表单的麻烦。

形式是能够保存,但不保存内容:

INSERT INTO "reviews" ("reviewer_id", "reviewee_id", "created_at","updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["reviewer_id", 1], ["reviewee_id", 15], ["created_at", 2016-09-16 09:52:49 UTC], ["updated_at", 2016-09-16 09:52:49 UTC]] 

这是用户模式:

has_many :active_reviews, class_name: "Review", 
          foreign_key:"reviewer_id", 
          dependent: :destroy 
has_many :passive_reviews, class_name: "Review", 
          foreign_key:"reviewee_id", 
          dependent: :destroy 
has_many :reviewing, through: :active_reviews, source: :reviewee 
has_many :reviewers, through: :passive_reviews, source: :reviewer 

def review(other_user, great) 
    active_reviews.create!(reviewee_id: other_user.id, content: great) 
end 

以及审查模式:

belongs_to :reviewer, class_name: "User" 
belongs_to :reviewee, class_name: "User" 
validates :reviewer_id, presence: true 
validates :reviewee_id, presence: true 

和Review控制器:

class ReviewsController < ApplicationController 
    before_action :logged_in_user, only: [:create, :destroy] 


    def create 
    @review = current_user.active_reviews.build 
    @user = User.find(params[:reviewee_id]) 
    current_user.review(@user, :content) 
    redirect_to reviews_user_path(@user) 
    flash[:success] = "User reviewed" 
    end 

    def destroy 
    end 

end 

意见(共享/ _review_form.html.erb):

<%= form_for(@review) do |f| %> 
     <%= render 'shared/error_messages', object: f.object %> 
     <div><%= hidden_field_tag :reviewee_id, @user.id %></div> 
     <%= f.text_area :content, placeholder: "Leave a review..." %> 
        <%= f.submit "Submit", class: "btn btn-primary" %> 
<% end %> 

视图(用户/ user_reviews.html.erb):

<% provide(:title, @title) %> 
<div class="row"> 
    <aside class="col-md-4"> 
    <section class="user_info"> 
     <%= gravatar_for @user %> 
     <h1><%= @user.name %></h1> 
     <span><%= link_to "view my profile", @user %></span> 
     <span><b>Microposts:</b> <%= @user.microposts.count %></span> 
    </section> 
    <section class="user_info"> 
     <%= render 'shared/review_form' %> 
    </section> 
    <% @reviews.each do |r| %> 
    <ol class="users follow"> 
     <li><%= r.reviewer.name %>: <%= r.content %></li> 
    </ol> 
    <% end %> 
</aside> 
</div> 
+0

我建议你阅读[如何提问的智能方式](http://catb.org/esr/faqs/smart-questions.html)。它会帮助你理解如何处理堆栈溢出等网站。我们不关心您的专业知识或经验,我们希望看到经过深思熟虑的问题,并得到很好的解答。 –

我认为你已经不叫 '保存'方法在你创建功能

如:

def create 

    #---your code---- 

    @reviews= Review.new(review_params) 

    if @review.save 
     redirect_to :action => 'list' 
    else  
     render :action => 'new' 
    end 

end 

个谢谢,我已经想通了

我的内容尚未插入,因为它是内部审核的哈希

"review"=>{"content"=>"fsdfsdfsdf"} 

溶液

def create 
    @user = User.find(params[:reviewee_id]) 
    @review = Review.create!(reviewer_id: current_user.id, reviewee_id:@user.id, content: params[:review][:content]) 
if @review.save 
    redirect_to reviews_user_path(@user) 
    flash[:success] = "User reviewed" 
end 
end 

def destroy 
end 
+1

请不要为你的问题添加答案,除非你打算选择这个作为官方答案。相反,感谢帮助你最多的回答者,通过提出答案和/或选择提供解决方案的答案。添加你的最终代码并没有达到这个目标,实际上并不鼓励。 –

+0

谢谢,我会记下来。 – Holmes