我有一个页面,上面有推荐列表。我在每个被设置为回复推荐的推荐上都有一个按钮。除了显示用户已成功回复推荐并在用户回复时切换按钮上的课程外,我不需要显示任何弹出窗口或表单。在回复推荐时,电子邮件(是表的索引)被传递,推荐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)
我知道这在控制器中一定很简单,我尝试使用助手但无处可去