将 user_id 添加到消息模型

发布于 2024-11-08 23:50:39 字数 1327 浏览 0 评论 0原文

我目前有一个简单的应用程序,其中包括通过设计进行用户身份验证和消息模型(消息模型使用 Jquery 和 Faye)。两者都独立工作正常,但我想在消息中添加 user_id。

我已经用关系更新了架构和模型,但在输入必要的代码以使视图和控制器将关系输入数据库时​​遇到问题,同时保持 jquery 工作。我不确定最好的方法,但在这里我尝试创建一个隐藏字段来提取 user_id,不确定这是否可能。这是适用的代码,感谢任何帮助。

消息 index.html.erb

<ul id="chat">
  <%= render @messages %>
</ul>

<%= form_for Message.new, :remote => true do |f| %>
<div class="field">
 <%= f.text_field :content %>
</div>
<div class="field">
 <%= f.hidden_field :user_id %>
</div>  
<div class="actions">
 <%= f.submit "Send" %>
</div>
<% end %>

create.js.erb 用于消息

 <% broadcast "/messages" do %>
 $("#chat").append("<%= escape_javascript render(@user.message) %>");
 <% end %>
 $("#new_message")[0].reset();

消息控制器

class MessagesController < ApplicationController
  def index
   if @authentications = current_user
    @messages = Message.all
   else
    redirect_to authentications_url
    flash[:notice] = "You need to sign in before answering questions"
   end
  end

 def create
  @user = User.find(params[:user_id])
  @message = @user.message.create(params[:message])
 end
end

我认为您拥有所需的一切,但如果没有,请告诉我,我将很乐意为您提供。

谢谢大家。

I currently have a simple app that includes user authentication through devise and a message model(the message model uses Jquery and Faye). Both are working fine independently, but I would like to add a user_id to the messages.

I have already updated the schema and models with the relationship, but I am having trouble inputting the necessary code to have the view and controller input the relationship into the db, while keeping the jquery working. I wasn't sure of the best way, but here I tried to create a hidden field that would pull the user_id, not sure if this is even possible. Here is the applicable code, any help is appreciated.

Message index.html.erb

<ul id="chat">
  <%= render @messages %>
</ul>

<%= form_for Message.new, :remote => true do |f| %>
<div class="field">
 <%= f.text_field :content %>
</div>
<div class="field">
 <%= f.hidden_field :user_id %>
</div>  
<div class="actions">
 <%= f.submit "Send" %>
</div>
<% end %>

create.js.erb for messages

 <% broadcast "/messages" do %>
 $("#chat").append("<%= escape_javascript render(@user.message) %>");
 <% end %>
 $("#new_message")[0].reset();

Messages Controller

class MessagesController < ApplicationController
  def index
   if @authentications = current_user
    @messages = Message.all
   else
    redirect_to authentications_url
    flash[:notice] = "You need to sign in before answering questions"
   end
  end

 def create
  @user = User.find(params[:user_id])
  @message = @user.message.create(params[:message])
 end
end

I think you have everything you would need, but if not, let me know and I will be happy to provide it for you.

Thanks, everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-11-15 23:50:39

有两件事需要纠正,

1)使用用户关联以表单创建消息实例(如果登录用户创建消息,则可能是 current_user)

<%= form_for user.messages.new, :remote => true do |f| %> #// assuming its has many association

2)如果是 has_many 关联,则在创建操作中更改关联

@message = @user.messages.create(params[:message])

two things to correct,

1)use user association to create message instance in form(probably current_user if logged-in user create a message)

<%= form_for user.messages.new, :remote => true do |f| %> #// assuming its has many association

2) if it is has_many association then change association in create action

@message = @user.messages.create(params[:message])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文