开发者

jQuery welcome message that appears until closed once (Rails 3)

I'd like to present a first-time user with a welcome messag开发者_如何学Ce that appears until the user 'closes' the message one time. (via jQuery's hide method).

How can this best be accomplished? I'm using Rails 3.


Depending on the details of your site, I would recommend just using a cookie to check if the user has clicked on the close button or not. Roughly speaking, it would be something like this:

if ( ! $.cookie("has_seen_message") ) {
  // show the welcome message

  $("#welcome-message .close_button").click(function() {
    $("#welcome-message").hide();
    $.cookie("has_seen_message", 1);
  });
}

Then you don't need to do anything at all on the Rails side. No point in modeling/persisting UI behavior, unless it's really important.


In order for the welcome message to persist between actions, you'd need to save something in the database to record whether or not the user has closed the message. You could do this by adding a column to the User model:

#new_migration.rb
add_column :users, :display_welcome_message, :boolean, :default => true

Include a conditional statement in your layout/view to check whether it should display the welcome message:

<% if current_user.display_welcome_message %>
  <!-- put you welcome message here -->
<% end %>

Then when the user clicks the 'hide' button, you could have jquery call a controller action asynchronously:

$.post('/user/<%= current_user.id %>/close_welcome')

Then create a controller action that hides the message permanently

#users_controller.rb
def close_welcome
  current_user.update_attributes({:display_welcome_message => false})
end

You'd need to create a route for this action too:

#routes.rb
resources :users do
  post 'close_welcome', :on => :member
end

Now that I've typed this out it seems a bit long-winded, perhaps someone can come up with something much neater. If you were going to have many different messages, your User model could get very messy - I'd probably start a new MessageAction model the belongs_to User, with a boolean field for each message type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜