ruby - rails 4 redirecting to most recent created record? -


building simple messaging app, using tutorial, , wanted know if it's possible me redirect recent updated record in database?

application.html.haml:

= link_to 'messages', :conversations 

i want able change localhost:3000/messages , it'll automatically gets redirected recent message recorded.

routes.rb

resources :conversations   resources :messages end 

conversation.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)     where("(conversations.sender_id = ? , conversations.recipient_id =?) or (conversations.sender_id = ? , conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)   end  end 

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 @ %l:%m %p")   end end 

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 information needed, wanted see if possible.

i following:

routes.rb

get 'messages', to: 'messages#most_recent' 

message.rb

class message < activerecord::base   belongs_to :conversation   belongs_to :user   #create scope 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 @ %l:%m %p")   end end 

messages_controller.rb

def most_recent   @message = message.most_recent.first end 

then can use @message display recent message in messages/most_recent.html.erb view.

but if still want keep index controller action working, need add condition.

you need add separate route most_recent controller action

routes.rb

get 'messages/most_recent' #in case not necessary specify controller action to: 

then in index controller action

messages_controller.rb

def index   if 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 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -