Ruby on rails 创建一个按钮,用于提取表id和用户id

Ruby on rails 创建一个按钮,用于提取表id和用户id,ruby-on-rails,devise,haml,Ruby On Rails,Devise,Haml,我有一个页面,上面有一个推荐列表。我在每个转介上都有一个按钮,用于回复转介。我不需要任何弹出窗口或表单来显示,除了一条显示用户已成功回复推荐并在用户回复时切换按钮上的类的闪光消息。回复转介后,电子邮件(表的索引)被传递,referralid也被传递到回复表。我尝试过很多方法,但我在控制器方面一无所获。我在模型上创建了适当的关联,但在控制器逻辑中仍然没有为每个回复创建回复记录。以下是我的模型: 推荐模式 答复表的迁移 class CreateReplies

我有一个页面,上面有一个推荐列表。我在每个转介上都有一个按钮,用于回复转介。我不需要任何弹出窗口或表单来显示,除了一条显示用户已成功回复推荐并在用户回复时切换按钮上的类的闪光消息。回复转介后,电子邮件(表的索引)被传递,referralid也被传递到回复表。我尝试过很多方法,但我在控制器方面一无所获。我在模型上创建了适当的关联,但在控制器逻辑中仍然没有为每个回复创建回复记录。以下是我的模型:

推荐模式

答复表的迁移

class CreateReplies
给出错误的_reference.html.haml部分上的代码:

=链接到“回复”。html\u安全,回复到“转诊”路径(转诊id:referral.id,回复者id:current\u user.id)

我知道这在控制器中一定很简单,我尝试过使用助手,但没有任何结果

添加您的路由和控制器,我们可以给您一个更好的答案,但我猜这不起作用,因为您正在向路由发送电子邮件

电子邮件有句号(
),除非您对路由添加限制,否则可能会中断路由

尝试将您的路线更改为:

resources :referrals do
  member do
    put "reply_to_referral" # will give you referrals/:id/reply_to_referral
  end
end
现在将您的链接更改为
reply\u to\u reflection\u path(id:reflection.id,email:current\u user.email)
,该链接应显示为
/reflections/32/reply\u to\u reflection?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 %>

#添加一个CSS类

您的意思是要在助手上显示以下内容:@reflection\u reply=@reflection.reflection\u repress.create(params[:email])?也得到了这个错误:NoMethodError in Referrals#index undefined method'reply#u to#u referral'for#我还需要带有电子邮件和referral id的referralreply来写入referralreply表,我们可以从referral controller执行此操作吗?我不认为您可以通过模型通过一个控制器写入另一个控制器,但我想我们建立了关联,所以它应该工作?您编写的第一件事不是帮助器,而是应该在控制器
reply\u to\u reference
操作中的代码。第二件事应该是视图中的
reply\u to\u reference\u path(…)
(我打错了)。没错,你可以从任何控制器访问任何你想要的表,但显然你不想弄得乱七八糟,到处都是代码。好吧,我想#路径丢失了,好吧,在引用中仍然会出现NoMethodError#索引,ActionView::Template::Error(对于#的未定义方法`回复_至#引用#路径'):Routes.rb显示GemPort::Application.Routes.draw do resources:reflection\u reply resources:reflection do member do put“reply\u to\u reflection”end designe\u for:users,:controllers=>{:omniauth\u callbacks=>“users/omniauth\u callbacks”}end
@reflection\u reply=@reflection.reflection\u repress.create(params[:reflection])
不起作用,因为没有
params[:reflection]
,您实际上要创建什么?哪些属性?另外,请确保在更改路由后重新启动应用程序。我正在尝试将referral.id和current_user.email放入referral reply表。当前用户点击“回复”按钮时,每个转介可以有来自不同用户的多个回复(每个用户一个回复),但我将允许用户限制对转介的回复(比如说25)哦,我确实重新启动了服务器,仍然是同一个错误。我甚至可以从控制器中取出方法,但仍然收到错误。我已经更新了我的代码,向您展示如何保存这些属性。此外,您收到的错误与
索引
视图中的
\u reference.html.haml
部分有关,我会将它们都添加到您的问题。最后,在你的
转介
模型中,它应该是
有很多:转介回复
谢谢,好吧,所以我简化了名称,最终销毁了旧的脚手架模型/sql表/控制器,并创建了一个名为回复的新模型。我正在更新上面的代码,部分中给出的唯一问题是按钮,只要我把它拿出来,按一下回复键,就可以了。
  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
  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
    = 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)
resources :referrals do
  member do
    put "reply_to_referral" # will give you referrals/:id/reply_to_referral
  end
end
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
<% if referral.referral_replies.any? %>
  # add a CSS class
<% end %>