如何在使用1次后禁用提交按钮

时间:2014-06-27 08:48:50

标签: mysql ruby-on-rails ruby ruby-on-rails-4

我要做的是以下内容。

user.rb 可以 answer.rb company.rb 创建的几个 application.rb >。但是,用户每个唯一的应用程序只能回答一次。

我已经在模型中禁用了它,但无法弄清楚如何在视图中执行此操作。

我的答案控制员:

class AnswersController < ApplicationController
  before_action :authenticate_user!

 def show
  @application = Application.find(params[:id])
  @answer = Answer.new
end 


def create
  @answer = Answer.new(answer_params.merge(:user_id => current_user.id))
if @answer.save
  flash[:notice] = "You've successfully applied"
  redirect_to root_url
else
  flash[:alert] = "You've already applied"
  redirect_to root_url
end
end

private

 def answer_params
   params.require(:answer).permit(:answer_1, :answer_2, :answer_3, :application_id)
   end
 end
答案模型中的

我有一个存储的user_id。 现在我的想法是我们查看当前的答案:id并检查current_user.id是否存在于其中,如果是,我们禁用该按钮。但是我无法做任何成功的事情。

show.html.erb看起来像这样:

<%= form_for @answer do |f| %>
 <%= f.hidden_field :application_id, value: @application.id %>

     <p>Question 1: <%= @application.question_1 %></p>
     <%= f.text_area :answer_1 %>

    .......

    <%= f.submit "Submit" %>

 <% end %>

1 个答案:

答案 0 :(得分:0)

如果在Ruby中使用jQuery,you can use the .one() method

.one( events [, selector ] [, data ], handler )                [jQuery 1.7+]

events
    Type: String
    One or more space-separated event types and optional namespaces, such as "click" 
    or "keydown.myPlugin".
selector
    Type: String
    A selector string to filter the descendants of the selected elements that trigger
    the event. If the selector is null or omitted, the event is always triggered when
    it reaches the selected element.
data
    Type: Anything
    Data to be passed to the handler in event.data when an event is triggered.
handler
Type: Function( Event eventObject )
    A function to execute when the event is triggered. The value false is also allowed as 
    a shorthand for a function that simply does return false.