创建一个拉取表ID和用户ID的按钮

时间:2013-05-08 04:42:11

标签: ruby-on-rails devise haml

我有一个页面,上面有推荐列表。我在每个推荐上都有一个按钮,用于回复推荐。除了显示用户已成功回复推荐并在用户回复时切换按钮上的类时,我不需要任何弹出窗体或窗体显示除外。在回复推荐时,传递电子邮件(是表的索引),referralid也传递给回复表。我尝试了很多方法,但是控制器无处可去。我在模型上创建了正确的关联,但仍然无法在控制器逻辑中为每个回复创建回复记录。这是我的模特:

推介模式

  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

的routes.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

迁移回复表

  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)

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

1 个答案:

答案 0 :(得分: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 %>