Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery在Rails应用程序中没有反应_Javascript_Jquery_Html_Css_Ruby On Rails - Fatal编程技术网

Javascript jQuery在Rails应用程序中没有反应

Javascript jQuery在Rails应用程序中没有反应,javascript,jquery,html,css,ruby-on-rails,Javascript,Jquery,Html,Css,Ruby On Rails,我有一个表单,它正在创建要发送给收件人的消息,我正在尝试向其中添加一些jQuery,以使其更易于使用。 我想做的第一件事是,当用户按下Enter键时,提交表单并显示消息,而不重新加载页面,但仅当这个特定的文本区域被聚焦时。我现在有了一个脚本,但只有在点击页面上的“发送”按钮时才可以这样做 我添加了下面的代码(我是jQuery和javascript新手,所以可能不正确),但它在页面上没有反应。然后,我尝试了一些简单的操作,比如只改变css,比如文本区域的轮廓,但是什么也没有发生 所以我看到的问题是

我有一个表单,它正在创建要发送给收件人的消息,我正在尝试向其中添加一些jQuery,以使其更易于使用。 我想做的第一件事是,当用户按下Enter键时,提交表单并显示消息,而不重新加载页面,但仅当这个特定的文本区域被聚焦时。我现在有了一个脚本,但只有在点击页面上的“发送”按钮时才可以这样做

我添加了下面的代码(我是jQuery和javascript新手,所以可能不正确),但它在页面上没有反应。然后,我尝试了一些简单的操作,比如只改变css,比如文本区域的轮廓,但是什么也没有发生

所以我看到的问题是我的jQuery代码没有发生任何变化

下面是我在assets/javascript/中的“show.js”文件中的代码

$(document).ready(function() {
  $('#input_to_be_loaded').focus(function(){
    $('#input_to_be_loaded').css('outline-color','#ff0000');
  });

  $('#input_to_be_loaded').focus(function(){
    $(document).keydown(function(key) {
      switch(parseInt(key.which,10)) {

          // Enter key pressed
          case 13:
          /* get some values from elements on the page: */

          var $form = $("#message-form"),
          $submit = $form.find( 'button[type="submit"]' ),
          message_value = $form.find( 'input[name="micropost[content]"]' ).val(),
          recipient_value = $form.find( 'input[name="recipient[recipient_id]"]' ).val(),
          url = $form.attr('action');

          /* Send the data using post */
          var posting = $.post( url, { 
            recipient_id: recipient_value, 
            message: message_value 
          });
          $('#input_to_be_loaded').html("");
          break;
        }
      });
  });
});
以下是我认为的表格:

<%= form_for(@micropost, remote: true, :method => :POST, id: "micropost_form_id") do |f| %>
<%= f.hidden_field :recipient_id, :value => @user.id %>
<%= f.hidden_field :is_note, value: false %>

<%= render 'shared/error_messages', object: f.object %>
<div class="field">
    <%= f.text_area :content, placeholder: "Press enter to send message", value: "",class: "form-control-messages",  id: "input_to_be_loaded" %>
</div>
<%= f.submit "Send", class: "btn btn-info", style: "float: right;" %>
<% end %>
edit1:这是我的控制器代码:

class MicropostsController < ApplicationController
  protect_from_forgery except: :show
  before_action :signed_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy
  skip_before_action :verify_authenticity_token

  def create
    @micropost = current_user.microposts.build(micropost_params)
    @user= User.find_by_id(@micropost.recipient_id) 
    if @micropost.is_note
      @old_notes= current_user.notes(@user).first
      if @old_notes
        @old_notes.destroy
      end
      @old_notes= current_user.notes(@user).first
    end
    if signed_in?
      @conversation_items = current_user.conversation(@user).paginate(page: params[:page], :per_page => 7)
    end
    @micropost.new = true
    if @micropost.save
      #flash[:success] = "You just sent a message to #{User.find_by_id(@micropost.recipient_id).name}"
      respond_to do |format|
        format.html { redirect_to @user }
        format.js
      end

    else
      @feed_items = []
      redirect_to @user
    end
  end

  def destroy
    @user=User.find_by_id(@micropost.recipient_id)
    @micropost.destroy
    redirect_to @user
  end

  private

  def micropost_params
    params.require(:micropost).permit(:content, :recipient_id, :new, :is_note)
  end

  def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    redirect_to root_url if @micropost.nil?
  end
end
class MicropostsController7)
终止
@micropost.new=true
如果@micropost.save
#flash[:success]=“您刚刚向#{User.find_by_id(@micropost.recipient_id).name}发送了一条消息”
回应待办事项|格式|
format.html{将_重定向到@user}
format.js
终止
其他的
@feed_items=[]
将_重定向到@user
终止
终止
def销毁
@user=user.find_by_id(@microspost.recipient_id)
@微观破坏
将_重定向到@user
终止
私有的
def micropost_参数
参数require(:microspost).permit(:content,:recipient_id,:new,:is_note)
终止
def正确的用户
@micropost=当前用户.microposts.find_by(id:params[:id])
如果@microspost.nil,是否将\u重定向到root\u url?
终止
终止
浏览器控制台提供:


提前谢谢各位:)

我觉得你们把事情复杂化了。 这应该起作用:

$("#input_to_be_loaded").keydown(function (event) {
    if (event.which == 13) {
        event.preventDefault();
        var $form = $("#message-form"),
            $submit = $form.find('button[type="submit"]'),
            message_value = $form.find('input[name="micropost[content]"]').val(),
            recipient_value = $form.find('input[name="recipient[recipient_id]"]').val(),
            url = $form.attr('action');

        /* Send the data using post */
        var posting = $.post(url, {
            recipient_id: recipient_value,
            message: message_value
        });
        $('#input_to_be_loaded').html("");
    }
});

您在浏览器的开发者控制台中看到了什么?show.js是否在资产目录中?请显示您的控制器代码。show.js在assets/javascript/I中。我在这个页面上添加了控制器代码和控制台输出。现在看起来好多了!泰!我还添加了一个小小的css“测试”来改变背景颜色,现在可以了!但是,由于某些原因,表单尚未提交,当我按Enter键时,控制台中会显示此消息:“加载资源失败:服务器响应状态为404(未找到)”,您猜这可能来自何处?错误消息可能与未提交的表单无关,当我单击“发送”按钮时,会出现相同的消息,该按钮提交表单并显示消息。这可能是路由中的路由问题。rbI认为这是发布的URL问题。您是从表单上的action属性中获取该属性的,但是您没有在ruby中设置它。或者这是routes中的路由问题。r实际上,我在ruby控制台中收到此错误消息:ActionController::ParameterMissing(未找到参数:micropost):app/controllers/micropost_controller.rb:43:in
micropost_params'app/controllers/micropost_controller.rb:8:in
create'我如何请求jQuery在“micropost”散列中传递参数?
class MicropostsController < ApplicationController
  protect_from_forgery except: :show
  before_action :signed_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy
  skip_before_action :verify_authenticity_token

  def create
    @micropost = current_user.microposts.build(micropost_params)
    @user= User.find_by_id(@micropost.recipient_id) 
    if @micropost.is_note
      @old_notes= current_user.notes(@user).first
      if @old_notes
        @old_notes.destroy
      end
      @old_notes= current_user.notes(@user).first
    end
    if signed_in?
      @conversation_items = current_user.conversation(@user).paginate(page: params[:page], :per_page => 7)
    end
    @micropost.new = true
    if @micropost.save
      #flash[:success] = "You just sent a message to #{User.find_by_id(@micropost.recipient_id).name}"
      respond_to do |format|
        format.html { redirect_to @user }
        format.js
      end

    else
      @feed_items = []
      redirect_to @user
    end
  end

  def destroy
    @user=User.find_by_id(@micropost.recipient_id)
    @micropost.destroy
    redirect_to @user
  end

  private

  def micropost_params
    params.require(:micropost).permit(:content, :recipient_id, :new, :is_note)
  end

  def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    redirect_to root_url if @micropost.nil?
  end
end
$("#input_to_be_loaded").keydown(function (event) {
    if (event.which == 13) {
        event.preventDefault();
        var $form = $("#message-form"),
            $submit = $form.find('button[type="submit"]'),
            message_value = $form.find('input[name="micropost[content]"]').val(),
            recipient_value = $form.find('input[name="recipient[recipient_id]"]').val(),
            url = $form.attr('action');

        /* Send the data using post */
        var posting = $.post(url, {
            recipient_id: recipient_value,
            message: message_value
        });
        $('#input_to_be_loaded').html("");
    }
});