将 user_id 添加到消息模型
我目前有一个简单的应用程序,其中包括通过设计进行用户身份验证和消息模型(消息模型使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事需要纠正,
1)使用用户关联以表单创建消息实例(如果登录用户创建消息,则可能是 current_user)
2)如果是 has_many 关联,则在创建操作中更改关联
two things to correct,
1)use user association to create message instance in form(probably current_user if logged-in user create a message)
2) if it is has_many association then change association in create action