[英]rails 4 redirecting to most recent created record?
Building a simple messaging app, using this tutorial , and wanted to know if it's possible for me to redirect to the most recent updated record in the database? 使用本教程构建一个简单的消息传递应用程序,并想知道我是否可以重定向到数据库中的最新更新记录?
application.html.haml : application.html.haml :
= link_to 'Messages', :conversations
I want to be able to change this to localhost:3000/messages
and then it'll automatically gets redirected to the most recent message recorded. 我希望能够将其更改为
localhost:3000/messages
,然后它将自动重定向到记录的最新消息。
routes.rb routes.rb
resources :conversations do
resources :messages
end
conversation.rb talk.rb
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
messange.rb messange.rb
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
messages_controller.rb messages_controller.rb
def index
@conversations = Conversation.all
@messages = @conversation.messages
if @messages.length > 10
@over_ten = true
@messages = @messages[-10..-1]
end
if params[:m]
@over_ten = false
@messages = @conversation.messages
end
if @messages.last
if @messages.last.user_id != current_user.id
@messages.last.read = true;
end
end
@message = @conversation.messages.new
end
Not sure what information is needed, but just wanted to see if this was possible. 不知道需要什么信息,只是想看看是否有可能。
I would do the following: 我将执行以下操作:
get 'messages', to: 'messages#most_recent'
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
#Create a scope for get the most recent record
scope :most_recent, :order => "created_at DESC", :limit => 1
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
def most_recent
@message = Message.most_recent.first
end
Then you can use @message
for display the most recent message in a messages/most_recent.html.erb
view. 然后,您可以使用
@message
在messages/most_recent.html.erb
视图中显示最新消息。
But if you still want to keep your index
controller action working, then you will need to add a condition. 但是,如果您仍想使
index
控制器操作保持正常运行,则需要添加一个条件。
You will need to add a separate route for most_recent
controller action 您将需要为
most_recent
控制器操作添加单独的路由
get 'messages/most_recent' #In this case is not necessary to specify controller action with to:
Then in your index
controller action 然后在您的
index
控制器操作中
def index
if SOME CONDITION HERE
redirect_to messages_most_recent_path
else
@conversations = Conversation.all
@messages = @conversation.messages
if @messages.length > 10
@over_ten = true
@messages = @messages[-10..-1]
end
if params[:m]
@over_ten = false
@messages = @conversation.messages
end
if @messages.last
if @messages.last.user_id != current_user.id
@messages.last.read = true;
end
end
@message = @conversation.messages.new
end
end
def most_recent
@message = Message.most_recent.first
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.