0
在我的应用程序,用户可以对话,和谈话有传真。消息属于用户(作为作者)和对话。Rails:你如何创建一个新的嵌套资源?
我想创建一个新的消息。这是我现在在MessagesController中使用的代码。
def new
@user = current_user #currently logged in user
@conversation = Conversation.find(params[:id])
@message = @conversation.messages.build
end
def create
@conversation = Conversation.find(params[:conversation_id])
@message = @conversation.messages.build(params[:message])
if @message.save
redirect_to username_conversation(current_user, @message.conversation)
else
redirect_to root_url
end
end
params [:message]包含消息内容(“content”=>“我是Spartacus”)。
这不是工作(可能是因为创建一个新的消息时,我不指定用户/作者?)。我如何使这个工作Rails的方式?
谢谢。
编辑:好了,现在的工作。谢谢你的提示。 – alste
不要忘记接受! –