如何在feed中呈现多态注释?

时间:2015-03-19 17:55:00

标签: ruby-on-rails ruby model-view-controller controller polymorphic-associations

我在http://railscasts.com/episodes/406-public-activity?view=asciicast之后创建了一个Feed。

在那个提要中,我想说,

  

"用户名" 添加/更新了值"值名称" ,评论为&# 34;评论内容"

此部分创建"值名称" 部分:

  • 视图/ public_activity /评论/ _create.html.erb



added value
<% if activity.trackable %>
  <b><%= link_to activity.trackable.name, activity.trackable %></b>
<% else %>
  which has since been removed 
<% end %>
&#13;
&#13;
&#13;

此部分创建&#34;评论内容&#34; 部分:

  • 视图/ public_activity /估值/ _create.html.erb

&#13;
&#13;
with the comment
<% if activity.trackable %>
  <%= link_to activity.trackable.content %>
<% else %>
  which has since been removed 
<% end %>
&#13;
&#13;
&#13;

如果我向其中一个部分添加名称或内容,我会收到undefined method错误消息。我需要将哪些代码添加到他们的控制器或应用程序控制器中以使这样的工作成为可能?

理想情况下,评论&#34;评论内容&#34;。只会在创建评论的情况下生成,以便带有评论,不会被遗弃。

&#13;
&#13;
class CommentsController < ApplicationController
	before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

	def index
		@comments = @commentable.comments
	end

	def new
		@comment = @commentable.comments.new
	end

	def create
		@comment = @commentable.comments.new(comment_params)
		if @comment.save
			@comment.create_activity :create, owner: current_user
			redirect_to @commentable, notice: "comment created."
		else
			render :new
		end
	end

	def edit
		@comment = current_user.comments.find(params[:id])
	end

	def update
		@comment = current_user.comments.find(params[:id])
		if @comment.update_attributes(comment_params)
			redirect_to @commentable, notice: "Comment was updated."
		else
			render :edit
		end
	end

	def destroy
		@comment = current_user.comments.find(params[:id])
		@comment.destroy
		@comment.create_activity :destroy, owner: current_user
		redirect_to @commentable, notice: "comment destroyed."
	end

private
    def set_comment
      @comment = Comment.find(params[:id])
    end

	def load_commentable
		resource, id = request.path.split('/')[1, 2]
		@commentable = resource.singularize.classify.constantize.find(id)
	end

	def comment_params
		params.require(:comment).permit(:content, :commentable)
	end
end
&#13;
&#13;
&#13;

&#13;
&#13;
class ValuationsController < ApplicationController
  before_action :set_valuation, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @valuations = Valuation.tagged_with(params[:tag])
    else
      @valuations = Valuation.order('RANDOM()')
    end
  end

  def show
    @valuation = Valuation.find(params[:id])
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @valuation = current_user.valuations.build
  end

  def edit
  end

  def create
    @valuation = current_user.valuations.build(valuation_params)
    if @valuation.save
      redirect_to @valuation, notice: 'Value was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @valuation.update(valuation_params)
      redirect_to @valuation, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @valuation.destroy
    redirect_to valuations_url
  end

  private
    def set_valuation
      @valuation = Valuation.find(params[:id])
    end

    def correct_user
      @valuation = current_user.valuations.find_by(id: params[:id])
      redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
    end

    def valuation_params
      params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)
    end
end
&#13;
&#13;
&#13;

&#13;
&#13;
class ApplicationController < ActionController::Base
  include PublicActivity::StoreController
  #before_action :load_todays_habits
  before_action :set_top_3_goals
  before_action :randomize_value
  before_action :set_stats
  protect_from_forgery with: :exception
  include SessionsHelper

  def set_top_3_goals
    @top_3_goals = current_user.goals.unaccomplished.top_3 if current_user
  end

  def randomize_value
    @sidebarvaluations = current_user.valuations.randomize if current_user
  end

  def set_stats
    @quantifieds = Quantified.joins(:results).all
    @averaged_quantifieds = current_user.quantifieds.averaged if current_user
    @instance_quantifieds = current_user.quantifieds.instance if current_user
  end

    hide_action :current_user

  private 

  #def load_todays_habits
  #  @user_tags = current_user.habits.committed_for_today.tag_counts if current_user
  #  @all_tags  = Habit.committed_for_today.tag_counts if current_user
  #end

  # Confirms a logged-in user.
  def logged_in_user
    unless logged_in?
      store_location
      flash[:danger] = "Please log in."
      redirect_to login_url
    end
  end
end
&#13;
&#13;
&#13;

非常感谢你的时间。

0 个答案:

没有答案