0

我有一个页面,上面有推荐列表。我在每个被设置为回复推荐的推荐上都有一个按钮。除了显示用户已成功回复推荐并在用户回复时切换按钮上的课程外,我不需要显示任何弹出窗口或表单。在回复推荐时,电子邮件(是表的索引)被传递,推荐ID也被传递给回复表。我已经尝试了很多方法,但我对控制器无能为力。我在模型上创建了适当的关联,但在控制器逻辑中仍然无法为每个回复创建回复记录。这是我的模型:

推荐模型

  class Referral < ActiveRecord::Base
    attr_accessible :referraltype
    belongs_to :user
    validates :user_id, presence: true
    has_many :replies

    def nil_zero?
      self.nil? || self == 0
    end
  end

用户模型

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable, :omniauthable

      attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :provider, :uid, :image

      has_attached_file :image, styles: { medium: "320x320>", thumb: "50x50" }
      has_many :referrals
      has_many :replies
    end

回复控制器

  class RepliesController < ApplicationController

  end

回复模型

  class Reply < ActiveRecord::Base
    belongs_to :user
    belongs_to :referral
  end

推荐控制器

    class ReferralsController < ApplicationController
      before_filter :authenticate_user!

    def reply_to_referral
      @referral = Referral.find(params[:referral_id])
      @replier_id = params[:replier_id]

      @reply = @referral.replies.create(replier_id: @replier_id)
      flash[:success] = "Referral reply sent."
      redirect_to root_path
    end

      # GET /referrals
      # GET /referrals.json
      def index
        @referrals = Referral.order("created_at desc")
        @referrals

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @referrals }
        end
      end

      # GET /referrals/1
      # GET /referrals/1.json
      def show
        @referral = Referral.find(params[:id])

        respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @referral }
        end
      end

      # GET /referrals/new
      # GET /referrals/new.json
      def new
        @referral = current_user.referrals.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @referral }
        end
      end

      # GET /referrals/1/edit
      def edit
        @referral = current_user.referrals.find(params[:id])
      end

      # POST /referrals
      # POST /referrals.json
      def create
        @referral = current_user.referrals.new(params[:referral])

        respond_to do |format|
          if @referral.save
            format.html { redirect_to @referral, notice: 'Referral was successfully created.' }
            format.json { render json: @referral, status: :created, location: @referral }
          else
            format.html { render action: "new" }
            format.json { render json: @referral.errors, status: :unprocessable_entity }
          end
        end
      end

      # PUT /referrals/1
      # PUT /referrals/1.json
      def update
        @referral = current_user.referrals.find(params[:id])

        respond_to do |format|
          if @referral.update_attributes(params[:referral])
            format.html { redirect_to @referral, notice: 'Referral was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: "edit" }
            format.json { render json: @referral.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /referrals/1
      # DELETE /referrals/1.json
      def destroy
        @referral = current_user.referrals.find(params[:id])
        @referral.destroy

        respond_to do |format|
          format.html { redirect_to referrals_url }
          format.json { head :no_content }
        end
      end

    end

路由.rb

  GemPort::Application.routes.draw do
    resources :referrals do
      resources :replies
      member do
        put "reply_to_referral"
      end
    end

    devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

    root :to => 'pages#home'
    get 'about' => 'pages#about'
  end

Replies 表的迁移

  class CreateReplies < ActiveRecord::Migration
    def change
      create_table :replies do |t|
        t.references :user
        t.references :referral

        t.timestamps
      end
      add_index :replies, :user_id
      add_index :replies, :referral_id
    end
  end

_referral.html.haml 部分上给出错误的代码:

    = link_to '<i class="icon-ok icon-large pull-right icon-grey" rel="tooltip" title="Reply"> Reply</i>'.html_safe, reply_to_referral_path(referral_id: referral.id, replier_id: current_user.id)

我知道这在控制器中一定很简单,我尝试使用助手但无处可去

4

1 回答 1

0

添加您的路线和控制器,我们可以为您提供更好的答案,但我猜这不起作用,因为您正在向路线传递电子邮件。

电子邮件有句号 ( .),这可能会破坏您的路线,除非您向路线添加约束。

尝试将您的路线更改为:

resources :referrals do
  member do
    put "reply_to_referral" # will give you referrals/:id/reply_to_referral
  end
end

现在将您的链接更改为reply_to_referral_path(id: referral.id, email: current_user.email),这应该是/referrals/32/reply_to_referral?email=user@email.com

然后在引荐控制器中:

def reply_to_referral
  @referral = Referral.find(params[:id])
  @email = params[:email]
  # now make sure your referral_replies table has a column called 'email' and
  # also one called 'referral_id', then you can do:
  @referral_reply = @referral.referral_replies.create(email: @email)
  flash[:success] = "Referral reply sent."
  redirect_to # wherever required
end

您可以通过向路由添加约束来执行类似的操作,或者通过传入用户的 id 而不是电子邮件,然后查询数据库。

要设置按钮的样式,您可以检查推荐是否有任何回复:

<% if referral.referral_replies.any? %>
  # add a CSS class
<% end %>
于 2013-05-08T05:31:44.317 回答