在respond_to或redirect_to之后缺少模板

时间:2015-08-02 00:10:37

标签: javascript jquery ruby-on-rails json

我有完美的发布消息的方法:

def new_message
  #adding message to database
  tmp_message = Message.new  
  # Check if user is muted
  if current_user.role == 'muted'
    params[:message] = "пытался что-то сказать, но кляп оказался     сильнее"
  end
  # Check if the message is private
  if recipient = params[:message].match(/@(.+) (.+)/)
    # It is private, send it to the recipient's private channel
    @channel = "/messages/private/#{recipient.captures.first}"
    @message = { :username => current_user.username, :msg =>     recipient.captures.second }
  else
     # It's public, so send it to the public channel
    @channel = "/messages/public"
    @message = { :username => current_user.username, :msg =>     params[:message] }
  end
    tmp_message.channel = @channel
    tmp_message.message_text = params[:message]
    tmp_message.username = current_user.username
    tmp_message.save
  respond_to do |f|
     f.js
  end
end

然后,我复制了这个方法来处理来自数据库的数据:

def get_messages
 for i in 1..Message.count
  tmp_message = Message.find_by_id(i)
  if tmp_message
    if recipient = tmp_message.message_text.match(/@(.+) (.+)/)
    # It is private, send it to the recipient's private channel
    @channel = "/messages/private/#{recipient.captures.first}"
    @message = { :username => tmp_message.username, :msg =>     recipient.captures.second }
    else
    # It's public, so send it to the public channel
        @channel = "/messages/public"
    @message = { :username => tmp_message.username, :msg =>     tmp_message.message_text }
    end
  end
 end
     respond_to do |format|
    format.js { render nothing: true } 
     end

并在" room"方法:

def room
  redirect_to sign_up_path unless user_signed_in?
  if current_user.email == "madowley@gmail.com"
    current_user.role = "admin"
  end
    @queue = Array.new;
    @queue = queue_list
get_messages
end

get_messages.js看起来像:

// Clear message input
$('#message').val('');

// Send the message
<% publish_to @channel, @message %>

我试图添加&#34; format.html&#34;得到&#34;跳转错误,丢失块(产量)&#34;

1 个答案:

答案 0 :(得分:0)

很难测试您的解决方案。但是,我认为问题在于你要做的事情的渲染机制......还有一些需要考虑的事情:

  • 如果要在操作中运行方法,则应将渲染保留。
  • 您应该在过滤器中进行重定向,而不是在操作流程中进行重定向(请参阅下面的null
  • 您应该在每种方法中明确说明渲染,除非您希望有默认值..

为了解决这个问题,我认为你的控制器的一些重组应该是有序的:

var clientCollection = new Mongo.Collection(null);

正如我所说,没有测试过......让我们希望它有用。